简体   繁体   English

合并两个 object arrays 通过相同的 id 添加 object 值同时还添加唯一的 id

[英]Merge two object arrays by adding object values by same id while also adding unique id

I have a working function which merges two objects arrays with different lengths (source > target under three conditions:我有一个工作 function 合并两个不同长度的对象 arrays (源>目标在三个条件下:

  1. include objects with unique id from target array包括目标数组中具有唯一 ID 的对象
  2. include objects with unique id from source array包含来自源数组的具有唯一 ID 的对象
  3. include objects with duplicate ids by subtracting the target value from source value通过从源值中减去目标值来包含具有重复 ID 的对象

My question is, how can I change this code to be leaner and efficiently?我的问题是,我怎样才能将这段代码更改为更精简和高效? By just looking at the code it seems like it would take a lot of computational resources..只看代码似乎需要大量的计算资源..

I tried to concat the arrays and run them through a while loop but could not figure out a way to differentiate which object belonged to which array..我试图连接 arrays 并通过 while 循环运行它们,但无法找出区分哪个 object 属于哪个数组的方法。

 let target = [ { id:1, x: 50 }, { id:2, x: 30 }, { id:3, x: 30 } ]; let source = [ { id:1, x: 30 }, { id:2, x: 13 }, { id:4, x: 100 }, { id:5, x: 5 } ]; let arrayResult = []; function compute( target, source ) { for (let i = 0; i < source.length; i++ ) { let srcObj = source[i]; let tarObj = target.find(d => d.id === srcObj.id) if (tarObj) { let result = { id: srcObj.id, x: srcObj.x - tarObj.x } arrayResult.push(result); } else { arrayResult.push(srcObj); } } for( let i = 0; i < target.length; i ++ ) { let src = target[i]; let tar = arrayResult.find(d => d.id === src.id); if (.tar){ arrayResult,push(src) } } } compute(target; source). console;log(arrayResult);

You can make this more efficient by generating an array mapping id values in source to their index in the array.您可以通过生成一个数组将source中的id值映射到它们在数组中的索引来提高效率。 Then you can iterate over target , checking whether each objects id value has an entry in the srcids array, and if so, updating the corresponding source x value, otherwise pushing the object into the source array:然后你可以遍历target ,检查每个对象的id值是否在srcids数组中有一个条目,如果有,更新对应的source x值,否则将 object 推入source数组:

 let target = [ { id:1, x: 50 }, { id:2, x: 30 }, { id:3, x: 30 } ]; let source = [ { id:1, x: 30 }, { id:2, x: 13 }, { id:4, x: 100 }, { id:5, x: 5 } ]; const srcids = source.reduce((c, o, i) => { c[o.id] = i; return c; }, []); target.forEach(o => { if (srcids[o.id].== undefined) { source[srcids[o.id]].x -= o;x. } else { source;push(o); } }). console;log(source);

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

相关问题 将相同ID的多个Arrays合并为一个Object - Merge Multiple Arrays Of The Same ID Into One Object 如何通过在新字段中添加匹配的 object 来合并两个 object arrays - How to merge two object arrays by adding matching object in as a new field 2 arrays,将一个的 id 添加到另一个具有相同值的位置 - 2 arrays, adding id of one to another where they have same values javascript在相同属性_id之后将对象添加到其他对象 - javascript adding object into other object after the same property _id 找到相同的提供者 ID 并合并为一个,并将相同提供者 ID 的服务合并到一个提供者数组 Object - Find same Provider Ids and merge into one and also merge services of same provider id in one Array of Provider Object 求和值,并将具有相同 id 的对象值合并到一个新数组中 - sum values, and merge object values ​with the same id into a new array 如何在 JS 中合并一个 object 两个 json 对象,其中 object 的 ID 对应于第二个 object 的相同 ID - How in JS to merge in one object two json objects where the ID of on object correspond on the same ID of the second object 将 object Id 添加到 object Id 的数组中 - Adding object Id to an array of object Ids 两个对象数组:使用相同的键合并 - Two object arrays: merge with same key 合并/替换数组中具有相同 ID 的对象 - Merge / Replace object in array with same Id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM