1

Can anyone tell me why this isn't working? From what I can tell buy documentation and whatever examples I can find, it should, but I keep getting the error below

var colorArray = [];
    colorArray[0] = [];
    colorArray[0][0] = '2F76EE';
    colorArray[0][1] = '2F76EE';
    colorArray[0][2] = '5fff74';
    colorArray[0][3] = '5e6cff';
    colorArray[0][4] = 'a6ff1d';
    colorArray[1][0] = '2F76EE'; //error is happening here
    colorArray[1][1] = '2F76EE';
    colorArray[1][2] = '5fff74';
    colorArray[1][3] = '5e6cff';
    colorArray[1][4] = 'a6ff1d';

The Error when I run it

Uncaught TypeError: Cannot set property '0' of undefined

Thanks!

2
  • See stackoverflow.com/questions/966225/… Commented Mar 19, 2014 at 3:39
  • JavaScript doesn't have two dimensional arrays. It has arrays, and you can nest them. But just because you put a sub-array in the first slot of the main array does not mean you automatically get one in the second slot. Commented Mar 19, 2014 at 3:42

3 Answers 3

4

When you say colorArray[1][0], JavaScript accesses colorArray[1] and as it is not defined yet, it evaluates to undefined. So, you are trying to do undefined[0]. That is why it is failing.

To fix this,

  1. you have to initialize element 1 like this

    colorArray[1] = [];
    

    before doing any changes to that element

    colorArray[1][0] = '2F76EE';
    colorArray[1][1] = '2F76EE';
    ...
    
  2. Since you are doing static initialization, you can even do

    var colorArray = [];
    colorArray.push([ '2F76EE', '2F76EE', '5fff74', '5e6cff', 'a6ff1d' ]);
    colorArray.push([ '2F76EE', '2F76EE', '5fff74', '5e6cff', 'a6ff1d' ]);
    
  3. Otherwise, you can initialize straight away, like this

    var colorArray = [[ '2F76EE', '2F76EE', '5fff74', '5e6cff', 'a6ff1d' ],
                      [ '2F76EE', '2F76EE', '5fff74', '5e6cff', 'a6ff1d' ]];
    
  4. You can also do something like this

    var element1 = [ '2F76EE', '2F76EE', '5fff74', '5e6cff', 'a6ff1d' ];
    var element2 = [ '2F76EE', '2F76EE', '5fff74', '5e6cff', 'a6ff1d' ];
    var colorArray = [];
    colorArray.push(element1, element2);
    

    Note: You might be wondering, why I can't simply do

    colorArray.push(element1, element1);
    

    since both the arrays are the same. It will work, of course. But it has a problem. If you mutate one of the arrays it will affect others also. For example,

    var element1 = [ '2F76EE', '2F76EE', '5fff74', '5e6cff', 'a6ff1d' ];
    var colorArray = [];
    colorArray.push(element1, element1);
    colorArray[0].push("abcdef");
    console.log(colorArray);
    // [ [ '2F76EE', '2F76EE', '5fff74', '5e6cff', 'a6ff1d', 'abcdef' ],
    //   [ '2F76EE', '2F76EE', '5fff74', '5e6cff', 'a6ff1d', 'abcdef' ] ]
    

    You might not have expected this. But in JavaScript, all the variable names are just references to objects. So, when you do colorArray.push(element1, element1); you are adding the same reference twice. So, both the elements are pointing to the same object. So, mutating one will affect the other.

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

Comments

1

You need to have

colorArray[1] = [];

Comments

0

Far simpler than what you have, and also avoiding your problem:

var colorArray =
  [['2F76EE','2F76EE','5fff74','5e6cff','a6ff1d'],
   ['2F76EE','2F76EE','5fff74','5e6cff','a6ff1d']];

(See other answers for why what you wrote is broken.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.