BobDoleForPresident BobDoleForPresident - 4 months ago 7
Javascript Question

Trouble accessing values of array in javascript

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.

Answer

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]));
}

Comments