I am currently trying to use reduce to merge objects together when objects contain the a specific value attached to the machineId
key.
Beneath is the input:
var arr = [
{
"id":"4055",
"severity": "High",
"public": true,
"machineId": "48ed3",
"machineName": "powerb"
},
{
"id":"4045",
"severity": "High",
"public": true,
"machineId": "48ed3",
"machineName": "powerb"
},
{
"id":"3433",
"severity": "High",
"public": true,
"machineId": "43h5",
"machineName": "powerva"
}
]
Beneath is the expected output:
var output = [
{
machineId: "48ed3",
data: [
{
"id":"4055",
"severity": "High",
"public": true,
"machineId": "48ed3",
"machineName": "powerb"
},
{
"id":"4045",
"severity": "High",
"public": true,
"machineId": "48ed3",
"machineName": "powerb"
}
]
},
{
machineId: "43h5",
data: [
{
"id":"3433",
"severity": "High",
"public": true,
"machineId": "43h5",
"machineName": "powerva"
}
]
}
];
This is what I currently have as my solution:
const result = Array.from(new Set(cveId.map(s => s.machineId)))
.map(lab => {
return {
machineId: lab,
cveId: cveId.filter(s => s.machineId === lab).map(CVE => CVE.cveId)
}
})
console.log(result);
I've also tried creating an empty array and using !arr.contains() to push items into a new object but I'm struggling to get a working solution.
I'd appreciate any assistance with this.
Thanks.