FAQ: Arrays - Nested Arrays

Community%20FAQs%20on%20Codecademy%20Exercises

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

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

1 Like

Thanks, uncle for pointing that out!

1 Like