1

Im displaying all the files from upfiles array using each loop in jquery,

CODE using each loop:

$(upfiles).each(function(index3, filelist) {
    alert(filelist.name); //displays filename
    alert(filelist.size); //displays filesize
}

how can I do the same thing using for loop or while loop :

// **CODE I have tried :**
var i;
for(i=0;i<=upfiles.length;i++)
{
   alert(upfiles[i][name]);  // trying this way but not displaying
}
1
  • 1
    Use alert(upfiles[i]["name"]) or alert(upfiles[i].name) Commented Jul 1, 2014 at 9:40

2 Answers 2

3

Try:

for(var i = 0; i < upfiles.length; i++)
{
   alert(upfiles[i].name); // note the removal of the [] brackets
}

Special note: when using this for-loop you should loop while the variable i is less than the length of the array upfiles.length and not less or equal, because the array is 0-based, meaning you'll get an out-of-bound exception if i equals the length of the array.

Sign up to request clarification or add additional context in comments.

Comments

2

Try to pass the key value as quoted string while using bracket notation,

 alert(upfiles[i]['name']);

Full code:

for(var i=0;i<upfiles.length;i++)
{
   alert(upfiles[i]['name']);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.