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

How to turn objects in an array with the same keys into a seperate object of their own in the same array

I have an array of objects with the same keys, I want to create a new array of objects that creates a new object that combines the uniques ones together into one


const arrayOfObjects = [
{ 'Serial Number': '90946117350061093222581604839920' },
{ 'Serial Number': '77362117350061093222581604891733' },
{ 'Serial Number': '77362117350061093222581604891734' },
{ 'Part Number': 'TR40-60C779-AA' },
{ 'Part Number': 'ST41-60C780-AA' },
{ 'Part Number': 'QT41-60C780-AA' },
{ 'Cell Count': '28' },
{ 'Cell Count': '27' },
{ 'Cell Count': '29' },
{ 'Length': '10' },
{ 'Length': '11' },
{ 'Length': '11' },
]

So it needs to look like the below.

const desiredArrayOfObjects = [
{ 
    'Serial Number': '90946117350061093222581604839920',
    'Part Number': 'TR40-60C779-AA',
    'Cell Count': '27',
    'Length': '10',
},
{ 
    'Serial Number': '77362117350061093222581604891733',
    'Part Number': 'ST41-60C780-AA',
    'Cell Count': '28',
    'Length': '11',
},
{ 
    'Serial Number': '77362117350061093222581604891734',
    'Part Number': 'QT41-60C780-AA',
    'Cell Count': '29',
    'Length': '12',
},

]

I have tried it with the code below, but i’m clearly not getting it right, help is welcome and appreciated.

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

let newArr = [];
arrayOfObjects.map((x,i) => {
    Object.entries(x).map(([key, value]) => {
        if(!newArr.length){
            
            if(Object.keys(newArr[i]).toString() === key){
                newArr.push({[key]: value})                   
            }

        }
    })
})

>Solution :

I think it’best if you first separate them and the put it back together

const arrayOfObjects = [
{ 'Serial Number': '90946117350061093222581604839920' },
{ 'Serial Number': '77362117350061093222581604891733' },
{ 'Serial Number': '77362117350061093222581604891734' },
{ 'Part Number': 'TR40-60C779-AA' },
{ 'Part Number': 'ST41-60C780-AA' },
{ 'Part Number': 'QT41-60C780-AA' },
{ 'Cell Count': '28' },
{ 'Cell Count': '27' },
{ 'Cell Count': '29' },
{ 'Length': '10' },
{ 'Length': '11' },
{ 'Length': '11' },
]

const keys = ['Serial Number', 'Part Number', 'Cell Count', 'Length']

const separated = arrayOfObjects.reduce((res, item) => {
  const key = Object.keys(item)[0]
  const existing = res[key] || []
  res[key] = [...existing, item[key]]
  return res
}, {})

const result = separated[keys[0]].map((_, i) => {
  return keys.reduce((res, k ) => {
    return {...res, [k]: separated[k][i]}
  }, {})
})

console.log(result)
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