Solved

Javascript for next loop but in reverse

Posted on 2012-04-05
2
173 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
2 Comments
 
LVL 22

Accepted Solution

by:
Ivo Stoykov earned 500 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now