Question

I've got an object literal inside an array and I'm trying to access the keys: Name and Price.

Here is my object:

var ProductList = [  
    { "1": { Name: "Item name", Price: "0.99" } },  
    { "2": { Name: "Item name", Price: "0.99" } },  
    { "3": { Name: "Item name", Price: "8.99" } },  
    { "4": { Name: "Item name", Price: "8.99" } }  
  ];

The numbers 1,2,3,4 are the product item ID's, so I'm trying to do this:

console.log(ProductList[1].Name);

But it just doesn't seem to be working.

Thanks, Mark

Was it helpful?

Solution

try this: Since you are storing the hashes in an array. It seems, I could be wrong, that you want to access array elem position - 1 , per your product ID's. IF your ID's were varies, ie.. 123, 587, ABC987, then the following wouldn't work.

var ProductList = [

    {"1": {Name: "Item name", Price: "0.99"}},

    {"2": {Name: "Item name", Price: "0.99"}},

    {"3": {Name: "Item name", Price: "8.99"}},

    {"4": {Name: "Item name", Price: "8.99"}}

];


var prodRef = 1; // in this example

ProductList[prodRef - 1][prodRef].Name;

I would personally do the following, in setting up your object.

var ProductList = {

    "1": {Name: "Item name", Price: "0.99"},

    "2": {Name: "Item name", Price: "0.99"},

    "3": {Name: "Item name", Price: "8.99"},

    "4": {Name: "Item name", Price: "8.99"}

};

   // so, you can just always access it via your productID.
    var prodRef = 4;
    ProductList[prodRef].Name

OTHER TIPS

Recommended Approach

I would recommend modifying the objects in the array to include an ID attribute. With this attribute in place you can use the filter method on the array to find elements by their ID property.

var ProductList = [
    {ID: "11", Name: "Item name", Price: "0.99"},
    {ID: "22", Name: "Item name", Price: "0.99"},
    {ID: "33", Name: "Item name", Price: "8.99"},
    {ID: "44", Name: "Item name", Price: "8.99"}
];

function findById(arr, id){
    var results = arr.filter(function(e){
        return e.ID == id;
    });
    return (results.length) ? results[0]:undefined;

}

Orignal Array

If you must stick with the original array the following can be applied.

You must access the object using the property name associated with each object in the array:

console.log(ProductList[1]["2"].Name);

If you can't change the structure of these objects I would make a helper function:

var ProductList = [
{"1": {Name: "Item name", Price: "0.99"}},
{"2": {Name: "Item name", Price: "0.19"}},
{"3": {Name: "Item name", Price: "8.99"}},
{"4": {Name: "Item name", Price: "8.99"}}
];

function getInternal(arr, index){
    return arr[index][index+1];
}

var obj = getInternal(ProductList, 1);
console.log(obj.Name);

JS Fiddle: http://jsfiddle.net/68WDZ/

I think you may be looking for an Object instead of an Array.

var productList = {
    "1": { name: "Millenium Falcon", price: 20 },
    "2": { name: "Storm Troopers", price: 5.99 },
    ...
}

console.log(productList["1"].name == "Millenium Falcon");
console.log(productList["2"].price == 5.99);

Of course, if you cannot change the data you got, you must use productList[0]["1"].Name

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top