?
Solved

Javascript for next loop but in reverse

Posted on 2012-04-05
2
Medium Priority
?
180 Views
Last Modified: 2012-04-05
I have a loop.  But it's REMOVING invalid file extensions... see below.

var items = response.split("\n");
var myItemsOrgLength = items.length;
var myListOfExt = [];      
//alert(items.length);
var y;
//remove any items where the file extension is not in the validTypes list
for (x= items.length - 1; x >= 0; x--)
{
      fields = items[x].split("\t");
      // split filename into fields on the ".", then pop the last field from the stack: the file extension
      var ext = fields[1].split(".").pop();
            
            if (!contains(validTypes, ext))
      {
          myListOfExt[x] = ext;
          items.splice(x,1);
      }
    }
Just so you know, validTypes is a comma delimited strting from an XML file of acceptable file types.: example:  pdf,doc,docx,jpg,tif.... and so on.

The problem is, that the array doesn't start at ZERO but starts at 1, thus throwing off my array LENGTH.

Any thoughts?

here's the contains() function:

            function contains(array, value)
            {
              value = value.toUpperCase();
             
              for (var x= 0; x< array.length; x++)
                    if (array[x].toUpperCase() == value)
                          return true;
                    return false;
            }
0
Comment
Question by:pborregg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
Ivo Stoykov earned 2000 total points
ID: 37810648
just add a check whether item exists...

HTH

Ivo Stoykov

for (x= items.length - 1; x >= 0; x--)
{
// check whether it is valid item
      if(!items[0]) {  continue;  }
      fields = items[x].split("\t");

Open in new window

0
 

Author Closing Comment

by:pborregg
ID: 37811561
Cool
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question