# FAQ: Arrays - Nested Arrays

This community-built FAQ covers the “Nested Arrays” exercise from the lesson “Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## FAQs on the exercise Nested Arrays

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

can use array.indexOf in the nested array ???

There will be two or more lengths that we can discern from the nested array. The outer array will have a length, namely the number of elements. The inner arrays will have their own lengths, independent of the outer list.

``````a = [
['a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h', 'i'],
['j', 'k', 'l'],
['m', 'n', 'o', 'p', 'q', 'r'],
['s', 't', 'u'],
['v', 'w', 'x', 'y', 'z']
]
``````

The outer array has a length of `6`, and the inner arrays have their own length.

``a[3].length  =>  6``
5 Likes

numberClusters=[[1,2],[3,4],[5,6]]

const target=numberClusters[2][1]

console.log(target)

console.log(numberClusters.length)
console.log(numberClusters.indexOf(3))

when I am doing index of numberClusters the console prints -1.

is there a way to find the index of a nested array??

How would I assign a variable to access multiple indexes in the (nested array)?

Here is what the finished exercise looks like:

``````const numberClusters = [[1,2],[3,4],[5,6]];

const target = numberClusters[2][1];
console.log(target);
``````

Here is what I also tried:

``````const numberClusters = [[1,2],[3,4],[5,6]];

const target = numberClusters[2][1][0][0];
console.log(target);

const target = numberClusters[[2][1][0][0]];
console.log(target);
``````

Both of these are syntax errors.

If I wanted to declare a variable with two values of the array, would I need to use a function?

``````const numberClusters = [[1,2],[3,4],[5,6]];

//The values of the variables below determine your targets:
const whichNestedArray1 = 0;
const whichIndex1 = 1;
const whichNestedArray2 = 1;
const whichIndex2 = 0;

let target = (arr) => {
const y = arr[whichNestedArray1][whichIndex1];
const z = arr[whichNestedArray2][whichIndex2];
return target = [y,z];
};

target(numberClusters);
console.log(target);  //prints [2,3]
``````

This is what I came up with, and it appears to work (notice me senpai), but it feels a bit clunky. Is there a faster way to do it?

2 Likes

Thanks, uncle for pointing that out!

1 Like

`nestedArr[1]` log `[2, 3]` shouldn’t it log `2`

3 Likes
``````const numberClusters = [[1,2], [3,4], [5,6]];  //index 0 is [1,2], index 1 is [3,4], index 2 is [5,6]

const targetIndexOne = numberClusters[1];   //this references index 1 which is an array within an array. Index 1 is [3,4]

const targetIndexZero = numberClusters[0];  // This references index 0 which is [1,2]. Index 0 of this array is 1. Index 1 of this array is 2

const target2 = numberCluster[0][1] ;  // This will target the number 2
``````
1 Like

const numberClusters = [[1,2],[3,4],[5,6]];

const target = numberClusters[2][1];
console.log(target);

why does this point out the array that 6 belongs in?

Think of a nested array as a string, which is also subscriptable, so has an index the same as an array.

``````arr = ['one', 'two', 'three', 'four', 'five']
console.log(arr[2][4])
// e
``````
``````               arr[2]
[     ]
['one', 'two', 'three', 'four', 'five']
^
[4]
``````
``````const arrOfArrs = []
for (let x of arr) {
arrOfArrs.push(x.split(''))
}
console.log(arrOfArrs)
console.log(arrOfArrs[2][4])
``````
``````[ [ 'o', 'n', 'e' ],
[ 't', 'w', 'o' ],
[ 't', 'h', 'r', 'e', 'e' ],
[ 'f', 'o', 'u', 'r' ],
[ 'f', 'i', 'v', 'e' ] ]
e
``````

Of course! Not because the array is also an element means you can’t use array methods onto it

``````const numberClusters = [[1, 2], [3, 4], [5, 6]];
const target = numberClusters[2].indexOf(6);

console.log(target); // 1
``````

When I write the target variable, I am confused on why I have to add the `const target = numberClusters[2][1];`. What do the two numbers at the end mean?

Take it one step at a time. What is `numberClusters`? Well, it’s an array! To access elements in an array, we can use bracket notation wrapping the element index. So when we write `numberClusters[2]` we are accessing the 3rd element in the `numberClusters` array. Now, this element is an array itself and we want to access the second element in this nested array, so we write `numberClusters[2][1]` which will first access the 3rd element in the array (which is itself an array), then access the 2nd element in this nested array

2 Likes

In this example below, why does the 2nd log output ‘2’?

const nestedArr = [[1], [2, 3]];

console.log(nestedArr[1]); // Output: [2, 3]
console.log(nestedArr[1][0]); // Output: 2

In nestedArr[1][0], [1] refers to the index 1 of the array. [0] refers to the index 0 of the child array.