2

sorry if this question seems a little stupid but I have the following code:

var sides = {
    'red': [0,0,0,0,0,0,0,0,0],
    'ora': [0,0,0,0,2,0,0,3,0],
    'yel': [0,0,0,0,0,0,0,0,0],
    'gre': [0,0,0,1,0,1,0,0,0],
    'blu': [0,0,0,0,0,0,0,0,0],
    'whi': [0,0,0,0,0,0,0,0,0],
}

As an example how can I reference array item [0] in sides['red'] ?

I have tried:

sides['red'][0];
sides['red'[0]];

This is probably very wrong, can anyone suggest how I would declare it otherwise?

2
  • 2
    The first one should work. Commented Sep 19, 2014 at 11:25
  • sides['red'][0]; should be fine ... Commented Sep 19, 2014 at 11:26

2 Answers 2

5

Your first trial works as intended:

sides['red'][0];

This returns 0 as the first value (the 0 index) in your slides['red'] array is equal to 0:

'red': [0,0,0,0,0,0,0,0,0], ...
        ^

You could also use:

slides.red[0];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment, it must be my logic that isn't working and not the reference :S
1

Simple you can use . operator:

sides.red[0]

DEMO

var sides = {
    'red': [1,0,0,0,0,0,0,0,0],
    'ora': [0,0,0,0,2,0,0,3,0],
    'yel': [0,0,0,0,0,0,0,0,0],
    'gre': [0,0,0,1,0,1,0,0,0],
    'blu': [0,0,0,0,0,0,0,0,0],
    'whi': [0,0,0,0,0,0,0,0,0],
}

alert(sides['red'][0]);
alert(sides.red[0]);

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.