Your are creating an array of objects. If the items are inserted in order, you could use:
items[0].AMOUNT; // return the amount of the first item in the array
However, (using plain JavaScript) you may probably prefer to exploit the hashtable nature of JavaScript objects, and use something like this:
var items = {
item1: {
amount: 10
},
item2: {
amount: 20
}
};
Then you will be able to use either the subscript notation:
items['item1'].amount;
... or the dot notation:
items.item1.amount;
@casablanca's solution is a valid alternative, but note that the filter()
method runs in O(n) since the supplied selector is tested against each element of the array. On the other hand, an item from a hashtable can be found in O(1) (constant time).