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

Making an object from multiple properties

I have an array of objects like below :

var ara = [
 {
    Name: 'Lionel',
    Age: 32,
    Positions: ['Winger', 'Midfield', 'Striker']
 },
 {
    Name: 'Neymar',
    Age: 28,
    Position: ['Winger', 'Striker']
 }
]

I want this :

var profiles = [
    {
        Identity: {Name: 'Lionel', Age: 32},
        Positions: ['Winger', 'Midfield', 'Striker'],
    },
    {        
        Identity: {Name: 'Neymar', Age: 28},
        Positions: ['Winger', 'Striker']
];

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

>Solution :

You can simply use an .map() function and reformat your array

var ara = [
 {
    Name: 'Lionel',
    Age: 32,
    Positions: ['Winger', 'Midfield', 'Striker']
 },
 {
    Name: 'Neymar',
    Age: 28,
    Positions: ['Winger', 'Striker']
 }
]


var profiles = ara.map((obj) => {
  return {
    Identity:{
      Name: obj.Name,
      Age: obj.Age
    },
    Positions: obj.Positions
  }
})

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