salacis salacis - 1 month ago 12
Javascript Question

Using objects to store a list of names

I was wondering how to use an object to store a list of different names and access them by simply using the key.
Do I have to use embedded object like this.

var f =
{
0 : { name : "John" },
1 : { name : "Phillip" }
};
console.log(f[1].name);

Answer

Do not over-complicate things. Why don't you just try a simple array?

var f = [
    { name : "John" }, 
    { name : "Phillip" }
];
console.log(f[1].name);