简体   繁体   English

比较数组中的对象,合并具有相同属性值的重复项,将属性值添加到合并的对象中

[英]Compare objects in array, merge duplicates with same property value, add property values to the merged object

I have an array of objects and I want to merge objects if they have the same property in object key email.我有一个对象数组,如果它们在对象键电子邮件中具有相同的属性,我想合并对象。 The overlapping properties need to be added to merged object.重叠属性需要添加到合并对象中。 With new object keys it would be best.最好使用新的对象键。 This seems to be kind of complicated.这似乎有点复杂。

[ { email: 'one@xyz.de',
    SearchQuery: 'Abts, Tomma',
    SearchResult: 1 },
  { email: 'one@xyz.de',
    SearchQuery: 'Ernst, Max',
    SearchResult: 3},
  { email: 'one@xyz.de',
    SearchQuery: 'Sigmund Abeles ',
    SearchResult: 1 },
  { email: 'two@xyz.de',
    SearchQuery: 'Barlach',
    SearchResult: 4 } ]

The result should be something like结果应该是这样的

[ { email: 'one@xyz.de',
    SearchQuery: 'Abts, Tomma',
    SearchResult: 1 
    SearchQueryTwo: 'Ernst, Max',
    SearchResultTwo: 3
    SearchQueryThree: 'Sigmund, Abeles ',
    SearchResultThree: 1 },
    { email: 'two@xyz.de',
    SearchQuery: 'Barlach',
    SearchResult: 4 } 
]

It would be possible, but more difficult than it is worth, to have SearchResultOne , SearchResultTwo , SearchResultThree , etc., so it makes more sense to put it into an array:拥有SearchResultOneSearchResultTwoSearchResultThree等是可能的,但比它的价值更难,因此将其放入数组更有意义:

 const inp = [ { email: 'one@xyz.de', SearchQuery: 'Abts, Tomma', SearchResult: 1 }, { email: 'one@xyz.de', SearchQuery: 'Ernst, Max', SearchResult: 3}, { email: 'one@xyz.de', SearchQuery: 'Sigmund Abeles ', SearchResult: 1 }, { email: 'two@xyz.de', SearchQuery: 'Barlach', SearchResult: 4 } ]; const oup = inp.reduce((acc, o) => { const queryResult = acc.find(qr => qr.email == o.email); if(queryResult) { queryResult.results.push({SearchResult:o.SearchResult, SearchQuery: o.SearchQuery}) } else { let newQR = {email: o.email, results: [{SearchResult:o.SearchResult, SearchQuery: o.SearchQuery}]}; acc.push(newQR); } return acc; }, []); console.log(JSON.stringify(oup));

暂无
暂无

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

相关问题 使用相同属性值的数组将多个对象合并为单个对象 - Merge multiple objects into single object with making array of same property values 在对象数组中的属性中查找具有最大值的所有对象,并从同一对象返回其他属性的值 - Finding all objects with Max value in a property within an Array of Objects and return values of other property from the same object 我将如何防止对象数组中的重复项? 如果有一个具有相同属性的对象,它应该求和它的值 - How would I prevent duplicates in array of objects? And if there is a object with same property, it should sume its value 比较对象数组中的属性值 - Compare property value in an array of objects 将对象的属性值添加到对象数组 - Add property value from object to array of objects 根据 88222995402888 属性值从 javascript 对象数组中删除重复项 - Remove duplicates from javascript objects array based on object property value 查找具有相同属性值的对象并将它们添加到新的数组属性中 - Find objects with same property value and add them in new array property 比较并添加属性到 object 的数组 - Compare and add property to array of object 根据属性值删除对象数组中的重复项 - Removing Duplicates in Array of Objects based on Property Values 比较 object 的两个 arrays 具有相同的值,并在 Vue.js2 中将一个数组的属性转换/添加到另一个数组 - Compare two arrays of object with same values and chenge/add a property of one array to the other one in Vue.js2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM