简体   繁体   English

如何比较两个对象数组,如果值相同,则添加新键:Javascript 中第二个对象的值

[英]How to compare two array of object and if value are same add new key: value on second object in Javascript

Let's take an example.让我们举个例子。 I've two array of objects我有两个对象数组

let paymentType = [
  {id: "settle", set: "dis_10"},
  {id: "settle1", set: "dis_20"},
  {id: "settle2", set: "dis_30"},
]
let result = [
  {id: "settle", type: 40},
  {id: "settle4", type: 50},
  {id: "settle2", type: 60},
]

If id from both array matches I want to add the the set value to the result array of object.如果两个数组中的 id 都匹配,我想将设置值添加到对象的结果数组中。 Final result will be最终结果将是

let result = [
  {id: "settle", type: 40, set: "dis_10"},
  {id: "settle4", type: 50},
  {id: "settle2", type: 60},
]

I've tried using filter and some method, but didn't work as expected我试过使用过滤器和一些方法,但没有按预期工作

I'd reduce() everything to a single accumulator object keyed by id , and then get the values from that:我会将所有内容reduce()到一个由id键控的累加器对象,然后从中获取

 const paymentType = [ {id: "settle", set: "dis_10"}, {id: "settle1", set: "dis_20"}, {id: "settle2", set: "dis_30"}, ]; const result = [ {id: "settle", type: 40}, {id: "settle4", type: 50}, {id: "settle2", type: 60}, ]; const merge = (...arrays) => Object.values( arrays.reduce((a, array) => array.reduce((a, v) => { a[v.id] = {...(a[v.id] || {}), ...v }; return a; }, a), {}) ); console.log(merge(paymentType, result));

This has a linear runtime complexity ( O(n) ), as opposed to solutions that invoke find() or findIndex() on every iteration which are O(n log n) at best.这具有线性运行时复杂性 ( O(n) ),这与在每次迭代中调用find()findIndex()的解决方案相反,后者最多为O(n log n)

Hope it helps:希望能帮助到你:

 const paymentType = [ {id: "settle", set: "dis_10"}, {id: "settle1", set: "dis_20"}, {id: "settle2", set: "dis_30"}, ]; let result = [ {id: "settle", type: 40}, {id: "settle4", type: 50}, {id: "settle2", type: 60}, ]; result.forEach((resultItem, index) => { const id = resultItem.id; // Chech if the id exists in the paymentType array const paymentTypeIndex = paymentType.findIndex((paymentTypeItem) => paymentTypeItem.id === id); if(paymentTypeIndex > -1) { // If the id exists, we add the paymentType item values to the result item. resultItem.set = paymentType[paymentTypeIndex].set; } }); console.log(result);

this might be something that you are looking for if I understood your question well如果我很好地理解你的问题,这可能是你正在寻找的东西

 result.forEach((item) => {
      if (paymentType.find((el) => el.id == item.id))
        item.set = paymentType.find((el) => el.id == item.id).set;
    });

This will result in这将导致

[
    {
        "id": "settle",
        "type": 40,
        "set": "dis_10"
    },
    {
        "id": "settle4",
        "type": 50
    },
    {
        "id": "settle2",
        "type": 60,
        "set": "dis_30"
    }
]

暂无
暂无

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

相关问题 比较两个对象数组; 如果第二个对象包含与第一个对象相同的键/值,则返回第二个对象,否则从第一个对象添加键/值 - Compare two arrays of objects; If second object contains the same key/value as the first return the second's, if not add key/value from the first 如何在javascript中向数组添加新对象(键值对)? - How to add a new object (key-value pair) to an array in javascript? 如何在javascript中比较和更改对象值数组与对象键 - How to compare and change array of object value with object key in javascript Javascript根据键和值比较2个对象数组 - Javascript Compare 2 array of object based on key and value 如何根据条件向 JavaScript 中的 object 中的同一键添加新键值? - how to add a new key value to the same key in an object in JavaScript based on a condition? 如果 Javascript 中的值相同,如何比较并从数组中删除 object? - How to compare and remove the object from array if value is same in Javascript? 比较两个对象的 arrays,如果缺少某个键的值,则将具有该值的 object 添加到数组中 - 两种方式 - Compare two arrays of objects and if value for a certain key is missing, add an object with that value to the array - two ways 通过javascript中的特定值将数组与对象键/值进行比较 - Compare array with object key/value by specific value in javascript 如何在Javascript对象下添加新的键值对? - How to add new key-value pair under Javascript object? 如何在 JavaScript Object 中添加新的键值对 - How to add a new Key value pair in JavaScript Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM