Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript for next loop but in reverse

Posted on 2012-04-05
2
Medium Priority
?
184 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

610 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