I have the below sample array.(original array has more than 200 elements)
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"
]
}
]
Condition = if (client id && ancestor id are the same then merge the storyid) so the output should be like:
[{
"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"
]
}]
Please help me with javascript code to achieve this.
With plain Javascript, you could use a hash table as closure for grouping with clientid and ancestorid.
stroyid gets filtered for unique values.
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; }
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With