使用Javascript检查JSON对象是否包含值

34

我想检查一个JSON对象中是否包含某个键(key)的特定值(value),比如说我想检查在任何对象中,键(key) "name" 是否具有值为 "Blofeld"(这是正确的)。我该怎么做?

[ {
  "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 
} ]

2
“JSON对象”这种东西不存在。 - adeneo
obj.name === "Blofeld" - VLAZ
4个回答

55

您也可以使用Array.some()函数:

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>


1
非常感谢!这就是我选择的解决方案,它非常出色地工作了! - Rawland Hustle
我发现在这种情况下,some()比其他任何替代方案更易读。但是如果我错了,请纠正我,some()比filter()慢,对吗? - Sayan
4
我认为filtersome方法的速度差不多,都需要对每次迭代执行一个函数,但是some方法在它的函数第一次返回真时停止迭代。filter方法将遍历整个数组。因此,如果我需要布尔值回答是否至少有一个数组项满足给定条件,我会使用some;如果我需要从给定数组中检索子集数组,则会使用filter - Andriy
1
如果我需要在单个对象上应用而不是数组上应用怎么办? - WhoAmI
@WhoAmI,请检查我对你提出的问题的回答(https://stackoverflow.com/questions/53609028/read-and-push-object-in-another-object-without-array/54665299#54665299) - Andriy
显示剩余2条评论

16

这将返回一个数组,其中的元素符合 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);


谢谢回答我的问题!我采用了@Andriy的解决方案,所以没有尝试这个。 - Rawland Hustle
坚如磐石的答案! - Eajaz
如果我想要一个特定的记录,如果它包含该值,例如我希望返回包含该值而不是 true 和 false 的记录(对象数组中的特定对象),那么你会怎么做? - Rajat Agrawal

7
编写一个简单的函数,用于检查对象数组是否包含特定值。

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


谢谢回答我的问题!由于@Andriy的解决方案更简单/更短,我没有尝试它。 - Rawland Hustle
我同意Andriy有一个更简单的解决方案。我猜这是经验上的差距。 - call-me

2

通过简单循环遍历数组中的所有对象,使用 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接