2

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

4
  • ProductList[0]['1'].Name
    – Xotic750
    Feb 17, 2014 at 23:37
  • 3
    It has to be ProductList[0]["1"]["Name"]. Not sure why you'd set it up that way. Why wouldn't you have the objects like this instead: { ID: "1", Name: "Name", Price: "0.99" } ? Feb 17, 2014 at 23:38
  • @EfrainReyes If I set it up that way, how would I access the Name and Price from the object? Assuming I want to access by ID Feb 17, 2014 at 23:45

3 Answers 3

3

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
0
2

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/

3
  • 3
    "SyntaxError: Unexpected number": should be ProductList[1]["2"].Name or ProductList[1][2].Name
    – p.s.w.g
    Feb 17, 2014 at 23:39
  • Like @p.s.w.g. said, you need to make sure it understands that you're chaining the accessor.
    – royhowie
    Feb 17, 2014 at 23:41
  • @p.s.w.g Thanks for making me aware of that, I only tested the helper function which worked. Feb 17, 2014 at 23:46
1

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

3
  • I tried that earlier and got Uncaught SyntaxError: Unexpected token { at line 1 (var productList = {)for some reason. Hmm Feb 17, 2014 at 23:48
  • Sorry, I wrote 5,99 instead of 5.99. This should work...
    – Bart
    Feb 17, 2014 at 23:50
  • sorry Bart, it did work, what I had done was wrap each line in a new object, giving: var productList = { {"1": { name: "Millenium Falcon", price: 20 }}, {"2": { name: "Storm Troopers", price: 5.99 }}, ... } Feb 17, 2014 at 23:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.