【问题标题】:Use Javascript to check if JSON object contain value [duplicate]使用Javascript检查JSON对象是否包含值[重复]
【发布时间】:2017-03-19 05:49:08
【问题描述】:

我想检查如下 JSON 对象中的某个键是否包含某个值。假设我想检查任何对象中的键“名称”是否具有值“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 
} ]

【问题讨论】:

标签: javascript json object key


【解决方案1】:

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

}

【讨论】:

  • 当然,钥匙不见了。我刚刚编辑了它,谢谢!
【解决方案2】:

这将为您提供一个包含与 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 的解决方案,所以我没有尝试这个。
  • 坚如磐石的答案!
  • 如果我想要一个包含该值的特定记录怎么办,就像我希望记录(对象数组中的特定对象)包含的值不是真假而不是什么你会的
【解决方案3】:

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

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 的解决方案更简单/更短。
  • 我同意 Andriy 有一个更简单的解决方案。我猜这是经验上的差距。
【解决方案4】:

你也可以使用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)));
&lt;script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"&gt;&lt;/script&gt;

【讨论】:

  • 非常感谢!这是我所寻求的解决方案,效果非常好!
  • 我发现 some() 为此目的比任何其他替代方案更具可读性。虽然如果我错了请纠正我,some() 比 filter() 慢,对吧?
  • 我认为 filtersome 方法的速度大致相同,每次迭代都必须执行一个函数,但是,当函数返回 true 时,'some' 方法将停止迭代第一次。 filter 方法将迭代整个数组。所以,如果我需要布尔答案,至少有一个数组项满足给定条件,我会使用some,如果我需要从给定数组中检索子集数组,我会使用filter
  • 如果我需要应用在单个对象而不是数组上怎么办?
  • @WhoAmI,我建议使用lodashisEqual() 函数(lodash.com/docs/4.17.11#isEqual)。请参阅我更新的答案,其中找到了 objToFind1 而没有找到 objToFind2
猜你喜欢
  • 2018-11-21
  • 2015-01-24
  • 1970-01-01
  • 2021-12-19
  • 2014-06-17
  • 2019-05-27
  • 1970-01-01
  • 2022-11-09
  • 2019-08-24
相关资源
最近更新 更多