Use Javascript to check if JSON object contain value

104,236

Solution 1

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/[email protected]/lodash.min.js"></script>

Solution 2

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

Solution 3

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

Solution 4

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

}
Share:
104,236
Rawland Hustle
Author by

Rawland Hustle

Updated on September 30, 2021

Comments

  • Rawland Hustle
    Rawland Hustle over 2 years

    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 
    } ]