Unsure regarding return for [array[array][array]]]

So I’m working on potential class delineators(not a word I know, but it should be; I don’t want to use the word delineations!) for a potential game. A starting array to represent this is as follows:

let blueClasses = [['purple'['blue', 'blue']], ['midnight'['blue', 'black']], ['lightblue'['blue', 'white']], ['purple'['blue', 'red']], ['oak'['blue', 'green']]];

now when I try to create a variable for the mainColor, which is blue, via the code:

mainColor = blueClasses[3][0];

instead of getting result I expected, which was ‘blue’ as it grabbed that value from the array within the array, I am getting ‘undefined’.

I would love some help understanding why this is happening. And maybe even what to do instead moving forward? Thanks!

It’s hard to tell what you are trying to do here, that is not a valid JS data structure. If you mean this:

const blueClasses = [
  ['purple', ['blue', 'blue']],
  ['midnight', ['blue', 'black']],
  ['lightblue', ['blue', 'white']],
  ['purple', ['blue', 'red']],
  ['oak', ['blue', 'green']],
]

console.log(blueClasses[3][0])
// purple

or do you mean this:

const blueClasses = [
  ['purple', 'blue', 'blue'],
  ['midnight', 'blue', 'black'],
  ['lightblue', 'blue', 'white'],
  ['purple', 'blue', 'red'],
  ['oak', 'blue', 'green'],
]

console.log(blueClasses[3][0])
// purple

I was going for the first one. Brand new to arrays and trying to practice pulling out data from within the array and editing, etc. The one liner was due to copy pasting it from console.log, but do you mean the structure was wrong because I had one to many brackets? If I had done this in codepen jfiddle I see how much less confusing it may have been. Also, thank you.

I’m not sure if it is an issue with the console, but I tried it multiple times after changing the array code and continue to see ‘undefined’ as the returned value.

What is your current code that gives undefined?

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.