如何比较数组的元素和基于条件的合并?

我有下面的示例数组(原始数组有200多个元素)

var array = [ { "clientid": "ID000002", "accesstoken": "pllALEl3TlwLL9hHP938H", "groupname": "ABC", "ancestorid": "8982857550", "stroyid": [ "IGN-EXM-001-PDF", "IGN-EXM-002-PDF" ] }, { "clientid": "ID000002", "accesstoken": "pllALEl3TlwpHOD4aTP38H", "groupname": "EFG", "ancestorid": "4705872914", "stroyid": [ "APP-ENE-FIE-CON", "APP-ENE-ASS-INS", "APP-ENE-ASS-CAR", "APP-ENE-MAT-REA" ] }, { "clientid": "ID000002", "accesstoken": "pllALEl3TlwLL9hHP938H", "groupname": "ABC", "ancestorid": "8982857550", "stroyid": [ "IGN-EXM-001-ZIP", "IGN-EXM-002-ZIP" ] } ] 

条件=如果(客户端id &&祖先id是相同的然后合并storyid),所以输出应该是这样的:

 [{ "clientid": "ID000002", "accesstoken": "pllALEl3TlwLL9hHP938H", "groupname": "ABC", "ancestorid": "8982857550", "stroyid": [ "IGN-EXM-001-PDF", "IGN-EXM-002-PDF", "IGN-EXM-001-ZIP", "IGN-EXM-002-ZIP" ] }, { "clientid": "ID000002", "accesstoken": "pllALEl3TlwpHOD4aTP38H", "groupname": "EFG", "ancestorid": "4705872914", "stroyid": [ "APP-ENE-FIE-CON", "APP-ENE-ASS-INS", "APP-ENE-ASS-CAR", "APP-ENE-MAT-REA" ] }] 

请帮我用JavaScript代码来实现这一点。

       

网上收集的解决方案 "如何比较数组的元素和基于条件的合并?"

您可以使用下图合并storyId而不重复,然后将其replace为原始storyId数组:

 if(array[0].clientid == array[1].clientid && array[0].ancestorid == array[1].ancestorid){ var c = array[0].stroyid.concat(array[1].stroyid.filter(function (item) { return array[0].stroyid.indexOf(item) < 0; })); } 
 const myArray = [ { clientid: 1, ancestorid: 1, stroyid: ['a', 'b', 'c'] }, { clientid: 1, ancestorid: 1, stroyid: ['a', 'b', 'd', 'e'] }, ]; const newArray = myArray.reduce((newArray, entry) => { // Find if there is a duplicate in the new array const duplicate = newArray.find(newEntry => ( entry.clientid === newEntry.clientid && entry.ancestorid === newEntry.ancestorid )); // If there is a duplicate, merge their stroyids if (duplicate) { const cleanIDs = entry.stroyid.filter(id => !duplicate.stroyid.includes(id)); duplicate.stroyid = duplicate.stroyid.concat(cleanIDs); } else newArray.push(entry); // Else, add the entire entry return newArray; }, []); console.log(newArray); 

用简单的Javascript,你可以使用哈希表作为封闭与clientidancestorid分组。

stroyid得到独特的价值过滤。

 var array = [{ clientid: "ID000002", accesstoken: "pllALEl3TlwLL9hHP938H", groupname: "ABC", ancestorid: "8982857550", stroyid: ["IGN-EXM-001-PDF", "IGN-EXM-002-PDF", "dupe"] }, { clientid: "ID000002", accesstoken: "pllALEl3TlwpHOD4aTP38H", groupname: "EFG", ancestorid: "4705872914", stroyid: ["APP-ENE-FIE-CON", "APP-ENE-ASS-INS", "APP-ENE-ASS-CAR", "APP-ENE-MAT-REA"] }, { clientid: "ID000002", accesstoken: "pllALEl3TlwLL9hHP9n", groupname: "ABC", ancestorid: "8982857550", stroyid: ["IGN-EXM-001-ZIP", "IGN-EXM-002-ZIP", "dupe"] }], result = array.reduce(function (hash) { return function (r, a) { var key = [a.clientid, a.ancestorid].join('|'); if (!hash[key]) { hash[key] = { clientid: a.clientid, accesstoken: a.accesstoken, groupname: a.groupname, ancestorid: a.ancestorid, stroyid: a.stroyid }; return r.concat(hash[key]); } hash[key].stroyid = hash[key].stroyid.concat(a.stroyid).filter(function (temp) { return function (a) { return !temp[a] && (temp[a] = true); }; }(Object.create(null))); return r; }; }(Object.create(null)), []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

您可以采用的一种方法是遍历数组,并根据合并条件的唯一组合将结果添加到Map中。 像ids的串联应该工作。

如果地图在这个条件下已经有了一个对象作为它的关键字,我们将当前的项目与这个值合并,否则我们就添加它。

您的代码的一个瓶颈是,您还需要深入合并这些在JavaScript中本地不支持的对象,超出了这个问题的范围。 你可以看看这个线程 。

下面是一个例子(我正在使用deepmerge库进行合并):

 var array=[{clientid:"ID000002",accesstoken:"pllALEl3TlwLL9hHP938H",groupname:"ABC",ancestorid:"8982857550",stroyid:["IGN-EXM-001-PDF","IGN-EXM-002-PDF"]},{clientid:"ID000002",accesstoken:"pllALEl3TlwpHOD4aTP38H",groupname:"EFG",ancestorid:"4705872914",stroyid:["APP-ENE-FIE-CON","APP-ENE-ASS-INS","APP-ENE-ASS-CAR","APP-ENE-MAT-REA"]},{clientid:"ID000002",accesstoken:"pllALEl3TlwLL9hHP9n",groupname:"ABC",ancestorid:"8982857550",stroyid:["IGN-EXM-001-ZIP","IGN-EXM-002-ZIP"]}]; const map = new Map(); const mergeKey = ({ clientid, ancestorid }) => `${clientid}${ancestorid}`; for (const item of array) { const key = mergeKey(item); if (map.has(key)) { map.set(key, deepmerge(map.get(key), item)); } else { map.set(key, item); } } const merged = Array.from(map.values()); console.log(merged); 
 <script src="https://unpkg.com/deepmerge@1.3.2/index.js"></script>