Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
JavaScript reduce array of objects based on value

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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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; }

about 4 years ago · Juan Pablo Isaza Report

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; }

about 4 years ago · Juan Pablo Isaza Report

0

you 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'))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!