1

I have a simple problem with array, i can't solved.

Example : http://jsfiddle.net/8jyUT/

Why second console.log can't display the array correctly ?

(I'm on Chrome)

3
  • it's difference when you log array or value in array. Commented Apr 10, 2012 at 19:42
  • What do you mean by 'does not display correctly'? can you actually paste your code in the question as well as the expected results + actual results. Commented Apr 10, 2012 at 19:43
  • Stack Overflow is not a Mind Reader. Commented Apr 10, 2012 at 19:44

2 Answers 2

3

It's logging the array but it seems empty, because you didn't populate it numerically ( which is how arrays should be populated ). You can still access the name property of the array,

var albums = new Array();
var album = new Array();
album['name'] = 'This is War';
albums.push(album);

console.log(albums);            
console.log(albums[0].name);​

in JS, Any object can have properties, and arrays are objects. If you have no use for numerical ordering for the album nor any of the array methods, then use an object and not an array:

var albums = [], album = {};
album['name'] = 'Test';
albums.push(album)

console.log(albums);            
console.log(albums[0].name);​
Sign up to request clarification or add additional context in comments.

Comments

0

I was able to get the value to display by adding ['name']. I am not sure how much this helps. If you check on your current code it does show that it is an array, but you need to specific the second index for a value to display. If you are looking to do something with more properties, I would suggest an object not an array.

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.