35

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

4 Answers 4

56

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>

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks alot! This is the solution I went for and it worked brilliantly!
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?
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.
What if I need to apply on a single object instead of array ?
@WhoAmI, I would recommend to use lodash's isEqual() function (lodash.com/docs/4.17.11#isEqual). See my updated answer where objToFind1 is found whule objToFind2 is not.
|
17

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

3 Comments

Thanks for answering my question! I went with @Andriy's solution so I didn't try this one.
rock solid answer!
what if I want a particular record if it contains that value, like I want the record (particular object in the array of objects) to be returned if it contains that value not true and false than what will you do
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

2 Comments

Thanks for answering my question! I didn't try it since @Andriy's solution was simpler/shorter.
I agree that Andriy has a simpler solution. I gues it's the gap in experience.
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;
   }

}

1 Comment

Of course, the key was missing. I've just editted it now, thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.