简体   繁体   English

Javascript循环对象的槽数组,并返回一个仅包含其值已更改的键的对象

[英]Javascript loop trough array of object and return one object containing only keys that have their values changed

I have an array of objects I need to loop through and check if the key of the object has different value. 我有一个对象数组,我需要遍历并检查对象的键是否具有不同的值。 If so return this key. 如果是这样,请返回此密钥。

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 } }
];

expected result should be 预期结果应该是

const result = {
  last_name: true,
  age: true
}

PS: each object has always the same number of keys PS:每个对象始终具有相同数量的键

You can loop through the arrayOfObjects . 您可以遍历arrayOfObjects Check if current item 's keys have different value compared to previous object's item . 检查当前item的键与上一个对象的item是否具有不同的值。 If yes, set that key to true in result 如果是的话,设置键, trueresult

 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) 

Just use find . 只需使用find It's more clear 比较清楚

 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); 

You could take a check which looks into the nested object. 您可以检查一下嵌套对象。

 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); 

Here's a solution which keeps a flag for uniqueness, filters out any unique items, maps them to your desired format and creates an object of the resulting entries. 这是一个保留唯一性标志,过滤出任何唯一项,将它们映射为所需格式并创建结果条目的对象的解决方案。 Time complexity: O(n * length_of_item_with_most_keys), space complexity: O(n). 时间复杂度: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); 

You can also solve this with Array.reduce and Set where you would group on the key and keep adding the values. 您也可以使用Array.reduceSet 解决此问题,在该位置您可以对键进行分组并继续添加值。 Then filter on the size of the set ... if larger than 1 you had more than one value: 然后过滤集合的大小...如果大于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) 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM