0

I've got an array which has two levels (sorry if that's the wrong terminology) and I can access the first level but I want to go another level down.

Could someone point me in the right direction so I can access that data and output it?

Fiddle here - https://jsfiddle.net/ep2Lqzum/

var myStringArray = [
    ["a", "b", "c"],
  ["d", "e", "f"]
];

var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
  console.log(myStringArray[i]);
}
4
  • 5
    try this myStringArray[i][0]) it would in your case select a and d Commented Apr 10, 2019 at 9:07
  • 1
    The "level" is called a dimension. You have a 2-dimensional array - it can be n-dimensional. Commented Apr 10, 2019 at 9:14
  • Use a second for loop to iterate through the deeper level of the array Commented Apr 10, 2019 at 9:18
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 10, 2019 at 9:40

1 Answer 1

-1

Use another iteration inside the outer loop.

Here I use .foreach() but you can use any kind of loops you want.

Now you can access the deeper level of the array.

let myStringArray = [["a", "b", "c"],["d", "e", "f"]]
myStringArray.forEach(array => {
  console.log(array)
  array.forEach(character => {
    console.log(character)
  })
})

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

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.