swanson_ron
swanson_ron

Reputation: 183

Underscore JS, find object which contains value in array

Im looking for a solution to find json object which contains a value in b. example: find objects which contain "jinx" in b.

sample data. [{ id:1 a:"karma", b:["jinx","caitlyn","tristana"] }, {....}, {....}]

I understand underscore works better for key/value pairs but this would be of great help.

Thanks.

Upvotes: 3

Views: 6420

Answers (4)

Brett DeWoody
Brett DeWoody

Reputation: 62753

Underscore.js has a .contains() method which could useful in this case. If you're only concerned with searching the b key of your object then the following would work:

var sampleData = { id:1, a:"karma", b:["jinx","caitlyn","tristana"] };

if (_.contains(sampleData.b, 'jinx') {
  // Found
} else {
  // Not found
}

Based on your comment here's a revised version. This uses the .filter method of Underscore to filter the array to those containing jink in the b-keyed array.

var sampleData = [
  { id:1, a:"karma", b:["jinx","caitlyn","tristana"] },
  { id:2, a:"karma", b:["kinx","caitlyn","tristana"] },
  { id:3, a:"karma", b:["linx","caitlyn","tristana"] },
  { id:4, a:"karma", b:["minx","caitlyn","tristana"] },
  { id:5, a:"karma", b:["ninx","caitlyn","tristana"] },
  { id:6, a:"karma", b:["jinx","caitlyn","tristana"] },
  { id:7, a:"karma", b:["pinx","caitlyn","tristana"] },
  { id:8, a:"karma", b:["qinx","caitlyn","tristana"] },
  { id:9, a:"karma", b:["rinx","caitlyn","tristana"] }
];

var findJinx = function(data) {
  return _.first(_.filter(data, function(item) {
    if (_.contains(item.b, 'jinx')) {
      return item;
    }
  }));
}

console.log(findJinx(sampleData));

Upvotes: 2

Marko Mackic
Marko Mackic

Reputation: 2333

Here is my answer in pure js :

if(sample_data['b'].indexOf("jinx") != -1){
    console.log("Object in b contains jinx");
}

If you need to support older browsers you can use this polyfill

Array.prototype.indexOf || (Array.prototype.indexOf = function(d, e) {
    var a;
    if (null == this) throw new TypeError('"this" is null or not defined');
    var c = Object(this),
        b = c.length >>> 0;
    if (0 === b) return -1;
    a = +e || 0;
    Infinity === Math.abs(a) && (a = 0);
    if (a >= b) return -1;
    for (a = Math.max(0 <= a ? a : b - Math.abs(a), 0); a < b;) {
        if (a in c && c[a] === d) return a;
        a++
    }
    return -1
});

EDIT :

Since you said your data look like this

data=[{ id:1 ,a:"karma", b:["jinx","caitlyn","tristana"] }, {1:2}, {1:2}]
//between 1 and a you forgot ,

Then what you use is

if(data[0]['b'].indexOf("jinx") != -1){
    console.log("Object in b contains jinx");
}

Upvotes: 1

leona
leona

Reputation: 45

If you want to use underscore, you can use _.each to loop thru your list of objects, then use _.indexOf to check each b array in each object like so: http://jsfiddle.net/rkamelot6262/BwHxv/378/

Upvotes: 2

TimoStaudinger
TimoStaudinger

Reputation: 42460

This will give you an array out of objects that contains all items from array in where b contains the element jinx:

var in = [...];

var out = _.filter(in, function(item) {
   return _.contains(item.b, 'jinx');
});

Upvotes: 3

Related Questions