我有一个对象数组,需要遍历并检查对象的键是否具有不同的值。如果是这样,请返回此密钥。
const arrayOfObjects = [
{ item: { first_name: "Joe", last_name: "Dow", age: 15 } },
{ item: { first_name: "Joe", last_name: "d", age: 15 } },
{ item: { first_name: "Joe", last_name: "Dow", age: 20 } }
];
预期结果应为
const result = {
last_name: true,
age: true
}
PS:每个对象始终具有相同数量的键
答案 0 :(得分:3)
您可以遍历arrayOfObjects
。检查当前item
的键值是否与先前对象的item
的键值不同。如果是,请在true
result
const arrayOfObjects = [{ item: { first_name: "Joe", last_name: "Dow", age: 15 } }, { item: { first_name: "Joe", last_name: "d", age: 15 } }, { item: { first_name: "Joe", last_name: "Dow", age: 20 } }];
const result = {};
arrayOfObjects.forEach(({ item }, i, arr) => {
if (i === 0) return; // skip the first item
const prev = arr[i - 1].item; // get the previous item
for (const key in item) {
if (item[key] !== prev[key])
result[key] = true
}
})
console.log(result)
答案 1 :(得分:1)
只需使用find
。更清楚
const data = [
{ item: { first_name: "Joe", last_name: "Dow", age: 15 } },
{ item: { first_name: "Joe", last_name: "d", age: 15 } },
{ item: { first_name: "Joe", last_name: "Dow", age: 20 } }
];
let result = {};
for (var key in data[0].item) {
if (data.find((el) => el.item[key] !== data[0].item[key])) {
result[key] = true;
}
}
console.log(result);
答案 2 :(得分:0)
您可以检查一下嵌套对象。
function check(source, target, result) {
Object.entries(source).forEach(([k, v]) => {
if (v && typeof v === 'object') return check(v, target[k], result);
if (result[k] === undefined) result[k] = true;
result[k] = result[k] && v === target[k];
});
return result;
}
var arrayOfObjects = [{ item: { first_name: "Joe", last_name: "Dow", age: 15 } }, { item: { first_name: "Joe", last_name: "d", age: 15 } }, { item: { first_name: "Joe", last_name: "Dow", age: 20 } }],
result = arrayOfObjects.reduce((r, o, _, [q]) => check(q, o, r), {});
console.log(result);
答案 3 :(得分:0)
这是一个保留唯一性标志,过滤出任何唯一项,将它们映射为所需格式并创建结果条目对象的解决方案。时间复杂度:O(n * length_of_item_with_most_keys),空间复杂度:O(n)。
const arrayOfObjects = [
{ item: { first_name: "Joe", last_name: "Dow", age: 15 } },
{ item: { first_name: "Joe", last_name: "d", age: 15 } },
{ item: { first_name: "Joe", last_name: "Dow", age: 20 } }
];
const result = Object.fromEntries(
Object.entries(arrayOfObjects.reduce((a, e) => {
for (const k in e.item) {
if (!a[k]) {
a[k] = {val: e.item[k], unique: true};
}
else if (a[k].unique) {
a[k].unique = a[k].val === e.item[k];
}
}
return a;
}, {})
).filter(e => !e[1].unique).map(e => [e[0], true]));
console.log(result);
答案 4 :(得分:0)
您还可以使用Array.reduce和Set来解决此问题,您可以在其中分组键并继续添加值。然后根据集合的大小进行过滤...如果大于1,则说明您有多个值:
const data = [ { item: { first_name: "Joe", last_name: "Dow", age: 15 } }, { item: { first_name: "Joe", last_name: "d", age: 15 } }, { item: { first_name: "Joe", last_name: "Dow", age: 20 } } ];
let result = data.reduce((r,{item},i,a) => {
Object.keys(item).forEach(k => r[k] = (r[k] || new Set()).add(item[k]))
return i == (a.length-1)
? Object.keys(r).filter(k => r[k].size > 1).reduce((r,k) => (r[k] = true, r), {})
: r
}, {})
console.log(result)