views:

77

answers:

2

How do I get an item from a JavaScript object:

var items = [
  {
    ITEM:1,
    AMOUNT:10
  },
  {
    ITEM:2,
    AMOUNT:20
  }
];

I want to be able to do something like this:

$(items).filter(ITEM == 1).AMOUNT;

... which would return 10.

+1  A: 

You can use the Array filter method, which returns a new array containing all matching elements. (there could be more than one matching item)

var results = items.filter(function(obj) { return obj.ITEM == 1; });
for (var i = 0; i < results.length; i++)
  alert(results[i].AMOUNT);

Note that IE6 (I'm not sure about newer versions) doesn't support the filter method. You could always define it yourself if it doesn't exist:

if (typeof Array.prototype.filter == 'undefined')
  Array.prototype.filter = function(callback) {
    var result = [];
    for (var i = 0; i < this.length; i++)
      if (callback(this[i]))
        result.push(this[i]);
    return result;
  }
casablanca
What I ended up doing was a hashtable. I did it like this.$.each(JSONObject, function () { Hashtable[this.item] = this; });So now I can say:HashTable[OBJECT_I_WANT].VALUE_I_WANT_TO_SET = 'N';
colemande
+5  A: 

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).

Daniel Vassallo
This is close to what I want but the problem is that I don't know what the item value will be.
colemande
@colemande: Can you give an example? Note that using the subscript notation, you can still do `items['item' + 1].amount;`, where you can substitute the `1` with a variable... (`'item' + 1` is string concatenation, and returns `'item1'`)
Daniel Vassallo
I could use your first answer. But I need to be able to get the index of the object that I need. Any ideas how to do that?var items = [ { ITEM:red, AMOUNT:10 }, { ITEM:blue, AMOUNT:20 } { ITEM:green, AMOUNT:20 } ]; how can I get the index for items.ITEM == green?
colemande
@colemande: If you want the ability to reference an item by any attribute, then [@casablanca's solution](http://stackoverflow.com/questions/3507542/how-do-i-get-an-item-off-of-a-javascript-object/3507571#3507571) is definitely easier and more straightforward. You could still use the hashtable solution in my answer, but you would have to keep separate "indexes" for each attribute of your items. If you will be having hundreds of items, this could be worth exploring, but if you'll be having a few items, then you might want to test the `filter` solution.
Daniel Vassallo
I went with using a hashtable and that fixes the problem. Thanks for your help both of you.
colemande