[英]How to return the keys & values from two objects with different values
How would I loop over two objects and only return a list of the keys & values that were different?我将如何遍历两个对象并仅返回不同的键和值的列表?
let list = [];
let previousObject = {
key: 'key_1',
name: 'Previous name',
age: '30',
location: '12345 Main St.',
height: '77',
weight: '215',
...
}
let newObject = {
key: 'key_1',
name: 'New name',
age: '25',
location: '54321 Main St.',
height: '77',
weight: '195',
...
}
I would like the list to return...我希望列表返回...
list = [{ name: 'New name', age: '25', location: '54321 Main St.' }]
Things to consider:需要考虑的事项:
In your example list
is an array with only one object containing all the differences.在您的示例list
是一个数组,其中只有一个 object 包含所有差异。 To get that object, you can do this:要获得 object,您可以这样做:
let list = []; let previousObject = { key: 'key_1', name: 'Previous name', age: '30', location: '12345 Main St.', height: '77', weight: '215', } let newObject = { key: 'key_1', name: 'New name', age: '25', location: '54321 Main St.', height: '77', weight: '195', } let diff = {} for (const key in newObject) { if (previousObject[key].= newObject[key]) { diff[key] = newObject[key] } } console.log(diff)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.