简体   繁体   English

JavaScript:比较两个对象

[英]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'}
         ]
  1. Compare two objects ('original' and 'edited')比较两个对象(“原始”和“编辑”)
  2. If 'id' is set, compare the same ids' data, and take the data from 'edited', and get ONLY the 'id' and the data that is edited.如果设置了“id”,则比较相同的 ids 数据,并从“edited”中获取数据,并仅获取“id”和已编辑的数据。
  3. If 'id' is not set keep the whole data如果未设置“id”,则保留整个数据

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更新所有密钥对匹配

  • Validate the edited array id value is available in original array using Array#map and indexOf function使用Array#mapindexOf函数验证已编辑的数组 id 值在原始数组中是否可用
  • If not push entire object to new array如果没有将整个对象推送到新数组
  • First recreate the original array to object format like {key:[value]}首先将原始数组重新创建为对象格式,如{key:[value]}
  • Then match each key value pair match or not with forEach inside the reduce function然后将每个键值对与reduce函数内的forEach匹配或不匹配

 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.

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