简体   繁体   English

如果 json object 密钥在 javascript 中具有所有相同的值,如何检查并返回 true?

[英]How to check and return true if json object key is having all same values in javascript?

I have the following sample JSON object:我有以下示例 JSON object:

var data = [ {
  "id" : 1,
  "name" : "Abc",
  "age" : 30,
  "married" : true,
  "city": "ABC"
}, {
  "id" : 2,
  "name" : "Def",
  "age" : 25,
  "married" : true,
  "city": "ABC"
}, {
  "id" : 3,
  "name" : "Pqr",
  "age" : 28,
  "married" : false,
  "city": "ABC"
}, {
  "id" : 4,
  "name" : "Xyz",
  "age" : 40,
  "married" : true,
  "city": "ABC"
} ];

I want to return true and store it in a variable if all city key values are ABC only, or else it should return false (ie if one of city key values is not ABC ) from the given JSON object.如果所有city键值仅是ABC ,我想返回true并将其存储在变量中,否则它应该从给定的JSON object 返回false (即,如果city键值之一不是ABC )。 Can anyone please let me know on how to achieve this.谁能告诉我如何实现这一目标。 Thanks in advance.提前致谢。

Using Array#every :使用Array#every

 const data = [ { "id": 1, "name": "Abc", "age": 30, "married": true, "city": "ABC" }, { "id": 2, "name": "Def", "age": 25, "married": true, "city": "ABC" }, { "id": 3, "name": "Pqr", "age": 28, "married": false, "city": "ABC" }, { "id": 4, "name": "Xyz", "age": 40, "married": true, "city": "ABC" } ]; const valid = data.every(({ city }) => city === 'ABC'); console.log(valid);

Three possible ways of achieving this:实现这一目标的三种可能方法:

 let data = [{id:1,name:"Abc",age:30,married:,0:city,"ABC"}:{id,2:name,"Def":age,25:married,:0,city:"ABC"},{id:3,name:"Pqr",age:28,married:,1:city,"ABC"}:{id,4:name,"Xyz":age,40:married;.0.city;"ABC"}]. console.log(data;every(({ city }) => city === "ABC")). console.log(.data;some(({ city }) => city !== "ABC")); console.log(!data.filter(({ city }) => city !== "ABC").length);

simply:简单地:

data.filter(x => x.city === 'ABC')

Please do a more detailed search about the problem before opening a topic.请在打开主题之前对问题进行更详细的搜索。

data.some((el) => el.city !== "ABC")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查 object 数组中的所有值是否等于 false 并在 react 中返回 true - Check if all the values in array of object is equal to false and return true in react 如何知道所有 javascript 对象值是否为真? - How to know if all javascript object values are true? 如何获取 JSON object 中的所有密钥(javascript) - How to get all key in JSON object (javascript) 如何在 javascript 中检查具有相同 id 和 null 的 object 的数组 - How to check array of object having same id and null in javascript JavaScript | 遍历JSON对象并获取特定键的所有值 - JavaScript | Iterate through JSON Object and get all the values for a specific key 在javascript中返回对象的true和字符串值 - return object true and string values in javascript 以逗号分隔的字符串返回 object 值为 true 的键名 - Return object key names having value of true as comma delimited string 如何在Json文件中搜索和匹配2个输入字段值,如果找到匹配项,则从同一json对象返回第3个键值 - How to Search and Match 2 input fields values in Json file and if match found return 3rd Key Value from same json object 将 javascript Object 的所有 false 值设置为 true - Set all false values ​of javascript Object to true 如果数组的所有值都为真,如何返回真,否则返回假? - How to return true if all values of array are true otherwise return false?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM