Search code examples
arraysember.jscomputed-properties

Computed property for element in array


I'm trying to make a computed that should return true if a given string is present in an array.

My model has a property on it that is an array of user_id's that look something like this

"item_bid_history": [
  562,
  697,
  13193
]

I can get the currently logged in user's id which would be 13193

Currently I have something like this

bidding: Ember.computed('item.item_bid_history','userService.user_id',function(){
    return Ember.$.inArray(this.get('userService.user_id'),this.get('item.item_bid_history')) > -1;
})

Maybe I'm doing that wrong but even if I attempt to console.log the value of inArray I always get -1 even though the user_id is in some of the item's item_bid_history arrays

Is there maybe a macro that will do this for me or am I just not creating the computed properly?


Solution

  • Are you sure, the the this.get('userService.user_id') is 13193 (Number) and not "13193" (String). The $.inArray() wont find it, if it was typeof String.

    $.inArray(123, [123, 1234])
    //0
    
    $.inArray("123", [123, 1234])
    //-1