简体   繁体   English

如何使用javascript合并数组中的对象

[英]How to merge objects in an array using javascript

I want to merge objects present in an array.我想合并数组中存在的对象。

INPUT:输入:

[
   { 
     "email": "abc@gmail.com",
     "date": "2020-01-24",
     "name": "bc",
     "details": [{id:"1"}] 
   }, 
   { 
     "email": "xyz@abc.in",
     "date": "2020-01-22",
     "name": "de",
     "details": [{id:"1"}] 
   },
   { 
     "email": "efg@gmail.com",
     "details": [{id:2}]
   },
   { 
     "name": "ab",
     "email": "abc@gmail.com",
     "details": [{"id":"3"}] 
   },
   { 
     "name": "",
     "email": "xyz@abc.in",
     "age": "", 
     "details": [{id:"4"}]
   },
   { 
     "name": "ef",
     "email": "xyz@abc.in",
     "age": "",
     "details":[{id:"4"}]
   },
   { 
     "name": "",
     "email": "xyz@abc.in",
     "age":"18",
     "details":[{id:"4"}]
   } 
]

I want to merge based on email regex column(if I don't know the name of the field ie, it can be email or mail or any other name)我想基于电子邮件正则表达式列合并(如果我不知道该字段的名称,即它可以是电子邮件或邮件或任何其他名称)

(i)if "id" inside details is same and email is matching then need to do row merge ie, if any field in the first row is empty then copy it from 2nd or 3rd row to 1st row and now delete dupicate rows with same "id". (i)如果“id”内部详细信息相同并且电子邮件匹配,则需要进行行合并,即,如果第一行中的任何字段为空,则将其从第 2 行或第 3 行复制到第 1 行,然后删除相同的重复行“ID”。

(ii)If "id" inside details is different and email is same merge different objects into single object. (ii) 如果“id”内部详细信息不同且电子邮件相同,则将不同对象合并为单个对象。

Kindly help me in finding the solution.请帮助我找到解决方案。

In (ii) condition while merging columns with different id's need to get all the merged id's inside details array.在 (ii) 条件下,合并具有不同 id 的列需要获取所有合并后的 id 的内部详细信息数组。

EXPECTED OUTPUT预期输出

[
   {
     "email":"abc@gmail.com",
     "date": "2017-01-24",   
     "name": "bc",
     "age":"19",
     "details":[
                 {id:"1"},
                 {id:"3"}
                ]
   },
   { 
     "email": "xyz@abc.in",
     "date": "2017-01-22",  
     "name" :"ef",
     "age":"",
     "details": [
                  {id:1},
                  {id:4}
                ]
   },
   { 
     "email": "efg@gmail.com",
     "date": "",
     "name": "",
     "age": "",
     "details": [
                  {id:2}
                ]
   }
]

based on the list you've given this could be your solution.根据您提供的列表,这可能是您的解决方案。

(i)By looping over the inputList and checking if the (n) index exists in the newList it creates a new List without recurring emails. (i) 通过遍历 inputList 并检查 (n) 索引是否存在于 newList 中,它会创建一个新的 List 而不会重复发送电子邮件。

(ii)When the email is known it'll check if the id matches (is findable) in the newItem (newItem is the item in newList with the where the email matches the (n) index of the inputList) if it's found it'll continues. (ii)当电子邮件已知时,它将检查 id 是否在 newItem 中匹配(可找到)(newItem 是 newList 中的项目,其中电子邮件与 inputList 的(n)索引匹配)如果找到它'我继续。 If not it adds the item to the detail list.如果不是,它会将项目添加到详细信息列表中。

 const list = [ { email: "abc@gmail.com", date: "2020-01-24", name: "bc", details: [{ id: "1" }], }, { email: "xyz@abc.in", date: "2020-01-22", name: "de", details: [{ id: "1" }], }, { email: "efg@gmail.com", details: [{ id: 2 }], }, { name: "ab", email: "abc@gmail.com", details: [{ id: "3" }], }, { name: "", email: "xyz@abc.in", age: "", details: [{ id: "4" }], }, { name: "ef", email: "xyz@abc.in", age: "", details: [{ id: "4" }], }, { name: "", email: "xyz@abc.in", age: "18", details: [{ id: "4" }], }, ]; const mergeList = (list) => { let newList = []; list.forEach((item) => { let isExist = false; newList.forEach((newItem) => { if (item.email === newItem.email) { isExist = true; if (!newItem.details.find((detail) => detail.id === item.details[0].id)) newItem.details.push(item.details[0]); return; } }); if (!isExist) { newList.push(item); } }); return newList; }; console.log(mergeList(list));

const mergeList = (list) => {
  let newList = [];
  list.forEach((item) => {
    let isExist = false;
    newList.forEach((newItem) => {
      if (item.email === newItem.email) {
        isExist = true;
        if (!newItem.details.find((detail) => detail.id === item.details[0].id))
          newItem.details.push(item.details[0]);
      }
    });
    if (!isExist) {
      newList.push(item);
    }
  });
  return newList;
};


  [1]: https://jsfiddle.net/wcd7t93g/2/

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

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