admin管理员组文章数量:1391976
How to check if all objects in an array contains same keys and values
const arrOfObjects = [{a: 1, b: 2}, {a: 1, b: 2}, {a:1, b: 2 }] // true
const arrOfObjects = [{a: 1, b: 2}, {a: 1, b: 2}, {a:2, b: 1 }] //false
const arrOfObjects = [{a: 1, b: 2}, {a: 1, b: 2, c: 3}, {a:2, b: 1 }] //false
This is my trial that looks so ugly and bad and not working, i would be thankful if someone put an efficient code for that problem!
function test(arr){
const firstItem = arr[0];
const firstItemKeys = Object.keys(firstItem);
for(let i = 0; i < firstItemKeys.length; i++) {
for(let j = 0; j < arr.length; j++) {
for(let x in arr[j]) {
if(arr[j][x] !== firstItem[firstItemKeys[i]]) return false
}
}
}
return true
}
How to check if all objects in an array contains same keys and values
const arrOfObjects = [{a: 1, b: 2}, {a: 1, b: 2}, {a:1, b: 2 }] // true
const arrOfObjects = [{a: 1, b: 2}, {a: 1, b: 2}, {a:2, b: 1 }] //false
const arrOfObjects = [{a: 1, b: 2}, {a: 1, b: 2, c: 3}, {a:2, b: 1 }] //false
This is my trial that looks so ugly and bad and not working, i would be thankful if someone put an efficient code for that problem!
function test(arr){
const firstItem = arr[0];
const firstItemKeys = Object.keys(firstItem);
for(let i = 0; i < firstItemKeys.length; i++) {
for(let j = 0; j < arr.length; j++) {
for(let x in arr[j]) {
if(arr[j][x] !== firstItem[firstItemKeys[i]]) return false
}
}
}
return true
}
Share
Improve this question
asked Jun 18, 2020 at 23:50
Code EagleCode Eagle
1,2623 gold badges23 silver badges42 bronze badges
3
- 2 See stackoverflow./questions/201183/… for ways to check if objects are equal. Then just use one of those techniques in the loop. – Barmar Commented Jun 18, 2020 at 23:53
- 2 Some important restrictions for this question: Are outside libraries acceptable? Are the objects always a single level deep, or might they be nested? Can you confirm if values will always be scalar values? These will be important considerations for answering this to your requirements. – Alexander Nied Commented Jun 18, 2020 at 23:55
- @AlexanderNied no i don't want to use libraries, and no no nested objects and values not always scalar – Code Eagle Commented Jun 18, 2020 at 23:59
2 Answers
Reset to default 3Here is the code:
const arrOfObjects = [
{ a: 1, b: 2 },
{ a: 1, b: 2 },
{ b: 2, a: 1 },
]
function areEquals(a, b) {
var keys1 = Object.keys(a)
var keys2 = Object.keys(b)
if(keys1.length !== keys2.length) {
return false ;
}
for(key in a) {
if(a[key] !== b[key]) return false;
}
return true ;
}
function checkArray(arr) {
for (var i = 1; i < arr.length; i++) {
if (!areEquals(arr[0], arr[i])) return false
}
return true
}
console.log(checkArray(arrOfObjects))
If you can use lodash, then there is method _.isEqual
const _ = require('lodash')
const arrOfObjects = [{a: 1, b: 2}, {a: 1, b: 2}, {a:1, b: 2 }]
let isEqual = true
arrOfObjects.forEach(obj => {
if (!_.isEqual(arrOfObjects[0], obj)) {
isEqual = false
}
})
return isEqual
PS: This could be written in one line with reduce, but it will not be readable for anyone new to programming or javascript.
本文标签: javascriptHow to check if all objects in an array contains same keys and valuesStack Overflow
版权声明:本文标题:javascript - How to check if all objects in an array contains same keys and values? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744654957a2617909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论