3

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.

5
  • It's unclear, sample data is a variable so what about sample_data[b].indexOf("jinx"), if it returns -1 it means b doesn't contain jinx Commented Jun 20, 2016 at 23:53
  • let me try that out and update. thanks Commented Jun 21, 2016 at 0:09
  • I've posted in answer what I mean :) because it might not be clear enough in comment :) Commented Jun 21, 2016 at 0:11
  • Marko, each array object is of the format "{ id:1 a:"karma", b:["jinx","caitlyn","tristana"] }". let me know if that helps. Commented Jun 21, 2016 at 0:54
  • tell me, is sample data first instance string equal to "{ id:1 a:"karma", b:["jinx","caitlyn","tristana"] }" because if it's an object like you stated then it can't be created without , between 1 and a , what would it represent than. Commented Jun 21, 2016 at 1:02

4 Answers 4

3

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');
});
Sign up to request clarification or add additional context in comments.

2 Comments

works, thanks! only complain is that it returns an array of objects and not just the object. any idea? response = [object] instead of just object.
If you are only interested in the first result, replace _.filter with _.find.
2

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/

Comments

2

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

4 Comments

that would work for a single object but i forgot to mention that my data is an array. var sampleData = [{ id:1, a:"karma", b:["jinx","caitlyn","tristana"] }, {...}, {...}]
works, thanks! only complain is that it returns an array of objects and not just the object. any idea? response = [object] instead of just object.
So you're only expecting to find a single instance of the word you're searching for? Meaning only one of the objects will contain 'jinx'
If you know only 1 object contains you can simply return the first element from the resulting array. I've updated the solution using Underscores _.first method.
1

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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.