Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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.

>Solution :

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'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading