DEV Community

loading...

Discussion on: Manipulating Arrays!

Collapse
alonxx profile image
Alonso Diaz

what would be the best way to manipulate data from an array that is in a sub-array like in your examples?

Collapse
rthefounding profile image
Randy Rivera Author

Hi there! the best way to manipulate data from an array that is in a sub-array is quite simple but for now I'll keep it simple. Let's say I have a variable named myArray.

var myArray = [[1 ,2, 3], [4, 5, 6]];

As you can tell this is in a sub array.

Let's say we wanna change [[1,2,3] this guy but just the number 3, the number three is in index 2 because JavaScript count starts at 0,1,2 etc and the whole array [[1,2,3] is in the index 0 while the [4,5,6]] is index .
simply,

myArray[0][2] = 20
console.log(myArray); // will display [[1, 2, 20], [4, 5, 6]]