Solved

Javascript for next loop but in reverse

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

728 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