0

I have a list of objects in awards console.log(awards) looks like this..

[0]
[1]
count
next
previous
route
getRestangularUrl
getRequestedUrl
addRestangularMethod
clone
reqParams 
etc...

I want to loop each array of objects ignoring all the other stuff i.e just [0] and [1]. Now I can do this when I know how many objects there are however I could have 1 or 100s.

 for (var key in awards) {
    if (awards[0]){


         }
     if (awards[1]){


         }

 }

Is there a what in javascript to detect an array in the for look?

1
  • Are you asking how to tell if a variable contains a number? Commented Sep 8, 2014 at 10:23

3 Answers 3

1
var index=0;

    for (var value in awards) 
    {

        if (value instanceof Array) {
            alert('value is Array!');
         } else {
              alert('Not an array');
          }

         index=index+1;

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

2 Comments

can I get the array number form this i.e. 0, or 1
Use 'for(i=0;i<length; i++)' instead for optimized loop, rather than using both 'for in' and 'index' counter.
1

This should work !

var array = [1,2,3, [2,3], 4 , 5, [4,5]];

for (i in array){
  if (array[i] instanceof Array === false){
    console.log(array[i]);
  }
}

Have a nice develop day !

Comments

1
var i, length=awards.length;
for(i=0;i<length; i++){
    if(awards[i] instanceof Array){
        console.log("Array present at index " + i);
        console.log(awards[i]);
    }
}

Hope this is what you want. Let me know if this works for you.

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.