admin管理员组文章数量:1389903
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.
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.
Share Improve this question asked May 17, 2022 at 12:39 my name jeffmy name jeff 991 silver badge10 bronze badges 2-
The operation you are looking for is called "grouping" by
machineId
and can be achieved withArray.reduce
. Examples of doing that can be found here stackoverflow./questions/14446511/… – nbokmans Commented May 17, 2022 at 12:43 - 1 Does this answer your question? How can I group an array of objects by key? – evolutionxbox Commented May 17, 2022 at 12:44
4 Answers
Reset to default 2you can do this
const groupByKey = (data, key) => Object.values(
data.reduce((res, item) => {
const value = item[key]
const existing = res[value] || {[key]: value, data:[]}
return {
...res,
[value] : {
...existing,
data: [...existing.data, item]
}
}
}, {})
)
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"
}
]
console.log(groupByKey(arr, 'machineId'))
You could take a functiion with data and key and map later the entries.
const
groupBy = (data, key) => Object
.entries(data.reduce((r, o) => ((r[o[key]] ??= []).push(o), r), {}))
.map(([k, data]) => ({ [key]: k, data})),
data = [{ 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" }]
result = groupBy(data, 'machineId');
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
The initial grouping can be done using reduce
. To get the final values array use Object.values
on the reduce result
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" } ]
let res = Object.values(arr.reduce((acc,curr)=> {
acc[curr.machineId] = acc[curr.machineId] || {machineId:curr.machineId, data: []}
acc[curr.machineId].data.push(curr)
return acc
},{}))
console.log(res)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Try this solution:
const 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"}];
const output = [], machineIdx = {};
for (const e of arr) {
// No records for that machineId
if (typeof machineIdx[e.machineId] === 'undefined') {
// Create record for that machineId
machineIdx[e.machineId] = output.push({
machineId: e.machineId,
data: []
}) - 1;
}
// Spread operator to make a copy of the object
output[machineIdx[e.machineId]].data.push({ ...e });
}
console.log(output);
本文标签: JavaScript reduce array of objects based on valueStack Overflow
版权声明:本文标题:JavaScript reduce array of objects based on value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744593599a2614642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论