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!

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'}
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.

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

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'}];

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.