0

I cant seem to find a resource online for the syntax of multidimensional arrays, I was hoping someone here could identify the error, thanks. I'm storing the array like this:

songs={{'title':'I Like It','artist':'Enrique Englesias','url':'audio/I Like It.mp3'},
{'title':'Driving Me Crazy','artist':'Sam Adams','url':'audio/driving me crazy.mp3'},

Then im calling them like this

document.write(songs[0]['artist']);

Thanks in advance!

asked Nov 10, 2010 at 2:50
0

4 Answers 4

1

You're using { where you mean [.

Try:

 songs=[{'title':'I Like It','artist':'Enrique Englesias','url':'audio/I Like It.mp3'},
 {'title':'Driving Me Crazy','artist':'Sam Adams','url':'audio/driving me crazy.mp3'}
answered Nov 10, 2010 at 2:53
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

songs = [{'title':'I Like It','artist':'Enrique Englesias','url':'audio/I Like It.mp3'},
 {'title':'Driving Me Crazy','artist':'Sam Adams','url':'audio/driving me crazy.mp3'}];

...but note this is an array of two objects, not a multi-dimensional array.

answered Nov 10, 2010 at 2:53

Comments

1

Rewrite your songs array like so. This way you have an array of objects.

songs=[{'title':'I Like It','artist':'Enrique Englesias','url':'audio/I Like It.mp3'},
{'title':'Driving Me Crazy','artist':'Sam Adams','url':'audio/driving me crazy.mp3'}]

now you can do something like this

songs[0].title
answered Nov 10, 2010 at 2:54

Comments

0

That isn't an array. It's an object hash. And a malformed one at that. You could access it as desired, but it needs to look like this:

songs=[{'title':'I Like It','artist':'Enrique Englesias','url':'audio/I Like It.mp3'},
 {'title':'Driving Me Crazy','artist':'Sam Adams','url':'audio/driving me crazy.mp3'}];
answered Nov 10, 2010 at 2:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.