Solved

Javascript for next loop but in reverse

Posted on 2012-04-05
2
176 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call a function within the ASP code 4 19
key press alert 2 32
xjs: how to get sum of value in for loop 2 18
Implement a function similar to grep in vanilla Javascript 3 19
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

840 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