I have the following JSON:
[
{
"computers": {
"available": [
6,
2
],
"held": [],
"upgrades": [],
"installed": [
"PC-1"
]
},
"version": "0.6.55-0ubuntu12~20.04.4",
"name": "accountsservice",
"summary": "query and manipulate user account information"
},
{
"computers": {
"available": [
209,
211,
210
],
"held": [],
"upgrades": [],
"installed": [
"PC-3"
]
},
"version": "0.6.45-1ubuntu1.3",
"name": "accountsservice",
"summary": "query and manipulate user account information"
}
]
I want to make a check against the data to check to see if the name property has any duplicate values. As such above, there are 2 accountservices but they have different version values.
How would I combine the 2 so that I only have 1 accountservices but 2 version in it using ReactJS? The other data is not relevant so can either be removed or merged also, I am only interested in listing different version for each name.
Expected output:
[
{
"version": "0.6.55-0ubuntu12~20.04.4, 0.6.45-1ubuntu1.3",
"name": "accountsservice",
"summary": "query and manipulate user account information"
}
]
I have tried using the following function that I found online but can't get it working:
const sanitize = (source_array) => {
let output = [];
source_array.forEach( e => {
if (output.filter( e1 => e1.name === e.name ).length === 0) {
let unique_e = source_array.filter(j => j.name === e.name );
let model = {};
if (unique_e.length === 1)
model = e;
else {
model.name = e.name;
model.computers.installed = e.computers.installed;
model.version = [];
for ( const el of unique_e ) {
const { version } = el;
model.version.push({version});
}
}
output.push(model)
}
});
return output
}
