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:

Web Development

Introduction To JavaScript

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 (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.

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

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 ???

1 Like

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
6 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

1 Like
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?

1 Like

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
1 Like

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
1 Like

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.
:slightly_smiling_face:

Why is it called Nested Array and not Arrayception?

1 Like

That’s actually a very good question (anyone who has a better answer than the one I’m about to give please jump in LOL)!

Firstly, just an fyi, I’m not too familiar with Arrayception… expect that it’s a model problem that may or may not be related to associated arrays. (Feel free to elaborate on this haha :sweat_smile:) But back to the point, I always find naming an interesting concept. I’ve had similar questions only too often, but have been walking in circles for a direct answer. I guess sometimes the key is knowing that something works… I suppose “nested array” is an appropriate name though, because if the variable stores an array, that can be analogous to a container. Then imagine that inside the first container are two more containers that fit. And per the word’s definition, “nest” (as a verb) means “to fit,” so it’s like arrays are being fitted, or nested, inside broader arrays.

Ahh I realize this might be a lot to read at once, but I hope it helped! :sparkles:

1 Like

Yes, you can! I tried using .reverse() on an exercise of mine! Please check my example below: :wink:

// This is a nested array done by myself, where there are 3 nested arrays that have nested arrays within
const nestedArr3 = [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[11, 12, 13], [14, 15, 16], [17, 18, 19]], [[21, 22, 23], [24, 25, 26], [27, 28, 29]]];

console.log(nestedArr3[0][0], nestedArr3[1][1], nestedArr3[2][2]);
console.log(nestedArr3[2][2].reverse(), nestedArr3[1][1].reverse(), nestedArr3[0][0].reverse());

Hello!
I’m quite a newbie. Why doesn’t this work? I had the right answer in the end but just wanted to know why.

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

RIGHT ANSWER const target = numberClusters[2][1];

WRONG ANSWER: const target = numberClusters[2[1]];

These are newbie questions, so please bear with me!

What purpose do nested arrays serve? In what kind of situation would nested arrays be useful?