我必须比较两个未排序的对象数组,例如以下代码应该返回true:
compareObjs(
[{ foo: 'foo', bar: 'bar' }, { baz: 'baz' }],
[{ baz: 'baz' }, { foo: 'foo', bar: 'bar' }]
)
我知道有很多关于比较对象数组的答案,但我并没有真正找到用于比较未排序版本数组的明确答案。
答案 0 :(得分:0)
您可以使用JSON.stringify()将每个对象转换为字符串。然后排序这些数组。并比较每一对。
如果您不介意es6:
let arr1 = [{ foo: 'foo', bar: 'bar' }, { baz: 'baz' }];
let arr2 = [{ baz: 'baz' }, { foo: 'foo', bar: 'bar' }];
const Compare = (arr1, arr2) => {
if (arr1.length != arr2.length) {
return false
}
let a1 = arr1.map(e => JSON.stringify(e)).sort()
let a2 = arr2.map(e => JSON.stringify(e)).sort()
return !a1.map((e, i) => e == a2[i]).includes(false)
}
console.log(Compare(arr1, arr2))

答案 1 :(得分:0)
function givemeSanitizedObject(arr1) {
let obj = {}
arr1.forEach(function(element, index, array) {
obj = {
...obj,
...element,
}
})
return obj
}
function compareObjs(arr1, arr2) {
let obj1 = givemeSanitizedObject(arr1)
let obj2 = givemeSanitizedObject(arr2)
for (var property in obj1) {
if (Object.hasOwnProperty(property)) {
if (obj1.property !== obj2.property) {
return false
}
}
}
return true
}
console.log(compareObjs(
[{
foo: 'foo',
bar: 'bar'
}, {
baz: 'baz'
}], [{
baz: 'baz'
}, {
foo: 'foo',
bar: 'bar'
}]
))

答案 2 :(得分:0)
一种基本方法是循环遍历其中一个数组的所有对象,看看是否可以在另一个数组中找到类似的对象。
以下是一个例子:
function compareArrays(arr1, arr2){
if(arr1.length != arr2.length){
return false;
}
for(var i = 0; i < arr1.length; i++){
var value1 = arr1[i];
var keys1 = Object.keys(value1);
var found = false;
for(var j = 0; j < arr2.length; j++){
var value2 = arr2[j];
var keys2 = Object.keys(value2);
if(keys1.length == keys2.length
&& keys1.every(k => keys2.includes(k))
&& keys1.every(k => value1[k] == value2[k])){
found = true;
break;
}
}
if(!found){
return false;
}
}
return true;
}
var comp = compareArrays(
[{ foo: 'foo', bar: 'bar' }, { baz: 'baz'}],
[{ baz: 'baz' }, { foo: 'foo', bar: 'bar'}]
);
console.log(comp);
答案 3 :(得分:0)
您可以使用哈希表并检查属性和值是否相同。它适用于给定的类型。
function compareArrays(array1, array2) {
var hash = {};
if (array1.length !== array2.length) {
return false;
}
array1.forEach(function (o) {
var keys = Object.keys(o).sort(),
key = keys.join('|'),
value = keys.map(function (k) { return o[k]; }).join('|');
hash[key] = hash[key] || {};
hash[key][value] = (hash[key][value] || 0) + 1;
});
return array2.every(function (o) {
var keys = Object.keys(o).sort(),
key = keys.join('|'),
value = keys.map(function (k) { return o[k]; }).join('|');
return hash[key] && hash[key][value] && hash[key][value]--;
});
}
console.log(compareArrays([{ foo: 'foo', bar: 'bar' }, { baz: 'baz' }], [{ baz: 'baz' }, { foo: 'foo', bar: 'bar' }]));
console.log(compareArrays([{ foo: 'foo', bar: 'bar' }, { baz: 'baz' }, { baz: 'baz' }], [{ baz: 'baz' }, { foo: 'foo', bar: 'bar' }]));
console.log(compareArrays([{ foo: 'foo', bar: 'bar' }, { baz: 'baz' }, { foo: 'baz' }], [{ baz: 'baz' }, { foo: 'foo', bar: 'bar' }]));
console.log(compareArrays([{ foo: 'foo', bar: 'bar' }, { baz: 'baz' }, { foo: 'baz' }], [{ baz: 'baz' }, { foo: 'foo', bar: 'bar' }, { foo: 42 }]));
&#13;
答案 4 :(得分:0)
您可以尝试以下内容
这可以推广到任意数量的数组
const arr1 = [{ foo: 'foo', bar: 'bar' }, { baz: 'baz' }];
const arr2 = [{ baz: 'baz' }, { foo: 'foo', bar: 'bar' }];
// Method that sorts the object keys and builds a new object
// sortObj = (obj) =>
// Object.keys(obj).sort().reduce((a, b) => {a[b] = obj[b]; return a}, {})
// Method that sorts keys and builds a 2D array
sortObj = (obj) =>
Object.keys(obj).sort().map((key) => [key, obj[key]])
compareObj = (arr1, arr2) => {
if(arr1.length !== arr2.length)
return false;
// 1st array reduce
const reduceArr1 = arr1.reduce((a, b) => ({...a, ...b}), {});
// 2nd array reduce
const reduceArr2 = arr2.reduce((a, b) => ({...a, ...b}), {});
// Comparing the sortedObjects
return JSON.stringify(sortObj(reduceArr1)) === JSON.stringify(sortObj(reduceArr2))
}
console.log(compareObj(arr1, arr2))
答案 5 :(得分:0)
这是另一种可能性:
const array2 = [1,3,2,4,5];
const isInArray1 = array1.every(item => array2.find(item2 => item===item2))
const isInArray2 = array2.every(item => array1.find(item2 => item===item2))
const isSameArray = array1.length === array2.length && isInArray1 && isInArray2
console.log(isSameArray); //true