34

I want to check if a certain key in a JSON object like the one below contains a certain value. Let's say I want to check if the key "name", in any of the objects, has the value "Blofeld" (which is true). How can I do that?

[ {
  "id" : 19,
  "cost" : 400,
  "name" : "Arkansas",
  "height" : 198,
  "weight" : 35 
}, {
  "id" : 21,
  "cost" : 250,
  "name" : "Blofeld",
  "height" : 216,
  "weight" : 54 
}, {
  "id" : 38,
  "cost" : 450,
  "name" : "Gollum",
  "height" : 147,
  "weight" : 22 
} ]
wscourge
  • 10,657
  • 14
  • 59
  • 80
Rawland Hustle
  • 781
  • 1
  • 10
  • 16

4 Answers4

55

you can also use Array.some() function:

const arr = [
  {
    id: 19,
    cost: 400,
    name: 'Arkansas',
    height: 198,
    weight: 35 
  }, 
  {
    id: 21,
    cost: 250,
    name: 'Blofeld',
    height: 216,
    weight: 54 
  }, 
  {
    id: 38,
    cost: 450,
    name: 'Gollum',
    height: 147,
    weight: 22 
  }
];

console.log(arr.some(item => item.name === 'Blofeld'));
console.log(arr.some(item => item.name === 'Blofeld2'));

// search for object using lodash
const objToFind1 = {
  id: 21,
  cost: 250,
  name: 'Blofeld',
  height: 216,
  weight: 54 
};
const objToFind2 = {
  id: 211,
  cost: 250,
  name: 'Blofeld',
  height: 216,
  weight: 54 
};
console.log(arr.some(item => _.isEqual(item, objToFind1)));
console.log(arr.some(item => _.isEqual(item, objToFind2)));
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
Andriy
  • 14,781
  • 4
  • 46
  • 50
  • 1
    Thanks alot! This is the solution I went for and it worked brilliantly! – Rawland Hustle Nov 05 '16 at 17:51
  • I find some() more readable for this purpose than any other alternative. Although correct me if I am wrong, some() is slower than filter() right? – Sayan Mar 22 '18 at 20:48
  • 4
    I think both `filter` and `some` methods are about the same speed, both has to execute a function for each iteration, however, 'some' method will stop iteration when its function returns true for the first time. `filter` method will iterate entire array. So, I would use `some` if I need boolean answer wether at least on of the array items satisfies given condition, and use `filter` if I need to retrieve a subset array from the given array. – Andriy Mar 22 '18 at 23:26
  • 1
    What if I need to apply on a single object instead of array ? – WhoAmI Dec 04 '18 at 09:36
  • @WhoAmI, I would recommend to use `lodash`'s `isEqual()` function (https://lodash.com/docs/4.17.11#isEqual). See my updated answer where `objToFind1` is found whule `objToFind2` is not. – Andriy Dec 04 '18 at 12:54
  • @Andriy thanks , but i don't want to use external library. I need solution by javascript. Can you have a look and check on my question regarding the same. https://stackoverflow.com/questions/53609028/read-and-push-object-in-another-object-without-array – WhoAmI Dec 05 '18 at 06:23
  • @WhoAmI, please check my answer for your mentioned question (https://stackoverflow.com/questions/53609028/read-and-push-object-in-another-object-without-array/54665299#54665299) – Andriy Feb 13 '19 at 08:11
16

This will give you an array with elements matching with name === "Blofeld" :

var data = [ {
  "id" : 19,
  "cost" : 400,
  "name" : "Arkansas",
  "height" : 198,
  "weight" : 35
}, {
  "id" : 21,
  "cost" : 250,
  "name" : "Blofeld",
  "height" : 216,
  "weight" : 54
}, {
  "id" : 38,
  "cost" : 450,
  "name" : "Gollum",
  "height" : 147,
  "weight" : 22
} ];

var result = data.filter(x => x.name === "Blofeld");
console.log(result);
kevin ternet
  • 4,514
  • 2
  • 19
  • 27
7

Write a simple function to check if an object array contains a specific value.

var arr = [{
  "name": "Blofeld",
  "weight": 54
}, {
  "name": "",
  "weight": 22
}];

function contains(arr, key, val) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i][key] === val) return true;
  }
  return false;
}

console.log(contains(arr, "name", "Blofeld")); //true
console.log(contains(arr, "weight", 22)); //true

console.log(contains(arr, "weight", "22")); //false (or true if you change === to ==)
console.log(contains(arr, "name", "Me")); //false
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
call-me
  • 686
  • 9
  • 18
2

With simple loop through all objects in an array, using hasOwnProperty():

var json = [...];
var wantedKey = ''; // your key here
var wantedVal = ''; // your value here

for(var i = 0; i < json.length; i++){

   if(json[i].hasOwnProperty(wantedKey) && json[i][wantedKey] === wantedVal) {
     // it happened.
     break;
   }

}
wscourge
  • 10,657
  • 14
  • 59
  • 80