0

I have an array in JavaScript that I receive from the back end and has the following structure:

scope.myArray = [{"0":[1,2,3,4],"1":[0,2,3,1,4]},{"0":[1,2,3,4],"1":[0,2,3,1,4]}]

I tried to retrieve some of the values by doing this:

for(var x=0;x<scope.myArray.length;x++){
    console.log("myArray is ....... " + JSON.stringify(scope.myArray[x].0))
}

and this

 for(var x=0;x<scope.myArray.length;x++){
    console.log("myArray is ....... " + JSON.stringify(scope.myArray[x]."0"))
}

but neither works.

Any help with this would be greatly appreciated. Thanks in advance.

1
  • you can use: JSON.stringify(scope.myArray[x]["0"])) or just [0] Commented Oct 6, 2015 at 15:40

4 Answers 4

3

You bracket notation, like this

var myArray = [{"0":[1,2,3,4],"1":[0,2,3,1,4]},{"0":[1,2,3,4],"1":[0,2,3,1,4]}]

for (var x = 0; x < myArray.length; x++) {
    console.log("myArray is ....... " + JSON.stringify(myArray[x][0]));
}

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

Comments

1

You should access the properties via bracket notation.

for (var x=0; x < scope.myArray.length; x++){
    console.log("myArray is ....... " + JSON.stringify(scope.myArray[x]['0']))
}

Comments

0

You created an array containing one object, with keys "0", "1", etc.

Comments

0

Just try this:

scope.myArray = [
    {
        "0": [1, 2, 3, 4],
        "1": [0, 2, 3, 1, 4]
    }, 
    {
        "3": [1, 2, 3, 4],
        "4": [0, 2, 3, 1, 4]
    }
];
scope.myArray.forEach(function(item){
     console.log("myArray is ....... " + JSON.stringify(item[0]))
 });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.