[英]JavaScript: Comparing two objects
I want to compare two objects to make a new object.我想比较两个对象来创建一个新对象。
original = [
{id: "A1", name: "Nick", age: 20, country: 'JP', code: 'PHP'}
]
edited = [
{name: "Mike", age: 30, country: 'US'},
{id: "A1", name: "Nick", age: 25, country: 'US', code: 'PHP'}
]
The final object I want is like below;我想要的最终对象如下所示;
final = [
{name: "Mike", age: 30, country: 'US'},
{id: "A1", age: 25, country: 'US'}
]
I've been trying this using filter, but I can't get desired result...我一直在尝试使用过滤器,但我无法得到想要的结果......
Try with Array#reduce
.尝试使用
Array#reduce
。
Updated with all key pair match更新所有密钥对匹配
Array#map
and indexOf
functionArray#map
和indexOf
函数验证已编辑的数组 id 值在原始数组中是否可用{key:[value]}
{key:[value]}
var original = [{id: "A1", name: "Nick", age: 20, country: 'JP'}]; var edited = [{name: "Mike", age: 30, country: 'US'},{id: "A1", name: "Nick", age: 25, country: 'US'}]; var ids_org = Object.keys(original[0]).reduce((a,b,c)=> (a[b]=original.map(a=> a[b]),a),{}); var res = edited.reduce((a, b) => { if (b.id) { Object.keys(b).forEach(i=>{ if(ids_org[i].indexOf(b[i]) > -1 && i != 'id') delete b[i]; }) a.push(b); } else { a.push(b); } return a; }, []); console.log(res)
Here is a function I wrote for comparing objects to their deepest level... the function recursively goes into objects and arrays and pushes every known value to a global components array which is copied into another array for comparing...这是我编写的用于将对象与其最深层次进行比较的函数...该函数递归地进入对象和数组并将每个已知值推送到全局组件数组中,该数组被复制到另一个数组中以进行比较...
var components = []; var p = { a: 3, b: { a: 1, b: 2, c: 3, d: [1,2,3,4,5] }, c: 3, d: [2,3,4,5,[1,2,3,4], {a: 3, b: 3}, 'hello world'], e: 'jdjdjdk', f: { c: 3, f: function cool() { return 'hello world'; } } } var wow = deep_check_object(p, Object.keys(p)); console.log(wow); components = []; //flattened array can be used for comparing another object function deep_check_object(obj, keys) { keys.forEach((key, index) => { if( typeof(obj[key]) === 'object' && Array.isArray(obj[key]) === false && obj[key] !== null ) { components.push(`${key}-object-${obj[key]}`); push_proto(key, 'object', obj[key]); deep_check_object(obj[key], Object.keys(obj[key])); } else if( typeof(obj[key]) === 'object' && Array.isArray(obj[key]) === true ) { components.push(`${key}-array-${obj[key]}`); push_proto(key, 'array', obj[key]); deep_array_check(key, obj[key]); } else { components.push(`${key}-single-${obj[key]}`); push_proto(key, 'single', obj[key]); } }); return components; } function deep_array_check(key, arr) { for(let i = 0; i < arr.length; i++) { if( typeof(arr[i]) === 'object' && Array.isArray(arr[i]) === false && arr[i] !== null ) { components.push(`${key}-object-${arr[i]}`); push_proto(key, 'object', arr[i]); deep_check_object(arr[i], Object.keys(arr[i])); } else if( typeof(arr[i]) === 'object' && Array.isArray(arr[i]) === true ) { components.push(`${key}-array-${arr[i]}`); push_proto(key, 'array', arr[i]); deep_array_check(key, arr[i]); } else { components.push(`${key}-single-${arr[i]}`); push_proto(key, 'single', arr[i]); } } } function push_proto(k, t, v) { return; //recursively push proto set }
use de structuring to extract out id from the object.使用解构从对象中提取出 id。 use lodash isEqual method to compare and later add back the id to the object.
使用 lodash isEqual 方法进行比较,然后将 id 添加回对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.