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 add an element to the object containing the elements of the object

How can i get a part of the object and put it in a new element in that object

I have an original Array like this

[
{
   stt:1,
   code: 24,
   name: 'n',
   age: 27,
   address: 'somewhere',
},
{
   stt:2,
   code: 82,
   name: 'jo',
   age: 23,
   address: 'tree',
},
{
   stt:3,
   code: 2,
   name: 'con',
   age: 23,
   address: 'somewhere',
},
]

array that i expected to be a result :

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

[
{
   stt:1,
   code: 24,
   name: 'n',
   employee: {
     age: 27,
     address: 'somewhere',
  }
},
{
   stt:2,
   code: 82,
   name: 'jo',
   employee: {
     age: 23,
     address: 'tree',
   }
},
{
   stt:3,
   code: 2,
   name: 'con',
   employee: {
     age: 23,
     address: 'somewhere',
   }
},
]

i tried to get first 3 element of object but dont know how to do next

>Solution :

you can use Array.map for that

like this

const transform = data =>data.map(
  ({age, address, ...rest}) => ({...rest, employee: {age, address}})
)

const data = [
{
   stt:1,
   code: 24,
   name: 'n',
   age: 27,
   address: 'somewhere',
},
{
   stt:2,
   code: 82,
   name: 'jo',
   age: 23,
   address: 'tree',
},
{
   stt:3,
   code: 2,
   name: 'con',
   age: 23,
   address: 'somewhere',
},
]

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