Home > Mobile >  Convert an array of object to a new array of object with keys in javscript
Convert an array of object to a new array of object with keys in javscript

Time:03-25

I have an array of objects in the format below and would like to transform it into a new array of objects using a property as a key. The key should be unique. See shape of the object below

const mockedList = [
  {
    email: '[email protected]',
    id: '5052',
    name: 'Java',
  },
  {
    email: '[email protected]',
    id: '5053',
    name: 'Python',
  },
  {
    email: '[email protected]',
    id: '5054',
    name: 'C#',
  },
  {
    email: '[email protected]',
    id: '5055',
    name: 'Javascript',
  },
];

I would like to transform this and get an array of objects with keys and values in this format.

[
   {
      email: '[email protected]',
      languages: [
         {
            email: '[email protected]',
            id: '5055',
            name: 'Javascript',
         },
         {
            email: '[email protected]',
            id: '5053',
            name: 'Python',
         },
     ]             
   },
   {
      email: '[email protected]',
      languages: [
         {
            email: '[email protected]',
            id: '5052',
            name: 'Java',
         },
        {
            email: '[email protected]',
            id: '5054',
            name: 'C#',
        },
     ]
   }
]

I've tried using map-reduce

const result = mockedList.reduce((r, a) => {
  r[a.email] = r[a.email] || [];
  r[a.email].push(a);
  return r;
}, Object.create(null));

But did not get the right shape of data

CodePudding user response:

You can do:

const mockedList = [{email: '[email protected]',id: '5052',name: 'Java',},{email: '[email protected]',id: '5053',name: 'Python',},{email: '[email protected]',id: '5054',name: 'C#',},{ email: '[email protected]', id: '5055', name: 'Javascript' },]

const mockedListHash = mockedList.reduce((a, c) => {
  a[c.email] = a[c.email] || { email: c.email, languages: [] }
  a[c.email].languages.push(c)
  return a
}, {})
const result = Object.values(mockedListHash)

console.log(result)

In case you want to clean the repeated emails within languages:

const mockedList = [{email: '[email protected]',id: '5052',name: 'Java',},{email: '[email protected]',id: '5053',name: 'Python',},{email: '[email protected]',id: '5054',name: 'C#',},{ email: '[email protected]', id: '5055', name: 'Javascript' },]

const mockedListHash = mockedList.reduce((a, c) => {
  a[c.email] = a[c.email] || { email: c.email, languages: [] }
  a[c.email].languages.push({
    id: c.id,
    name: c.name,
  })
  return a
}, {})
const result = Object.values(mockedListHash)

console.log(result)

CodePudding user response:

Here is another option with simple for loop

// Array
const mockedList = [
  {
    email: '[email protected]',
    id: '5052',
    name: 'Java'
  },
  {
    email: '[email protected]',
    id: '5053',
    name: 'Python'
  },
  {
    email: '[email protected]',
    id: '5054',
    name: 'C#'
  },
  {
    email: '[email protected]',
    id: '5055',
    name: 'Javascript'
  }
];

// Set new object
const newObj = {};

// Use regular loop
for(const el of mockedList) {
  // Use email as key
  // If key already exist, add info
  // to it's languages array
  if(newObj[el.email]) newObj[el.email].languages.push(el);
  else newObj[el.email] = {
    email: el.email,
    languages: [el]
  }
}

// Test
console.log(newObj);

// If you need just array of objects,
// without email as key, then transform it
const newArr = Object.keys(newObj).map((key) => newObj[key]);

// Test
console.log(newArr);

  • Related