skip to Main Content

i have an array (0,…,n) and with several object. The array can be of any length.

array = {{name: alice, age: 18, hobby: dance}, {name:bob, age: 27, hobby: cars}, {name: chloe, age:45, hobby: painting}};

Now I want a new array. This new array should look like that:

new array = {alice, bob, chloe};

I can merge this objects with a for-loop. But is there a simple join or merge function which I can use?

Thanks

2

Answers


  1. Just use map 😉

    let array = [{name: alice, age: 18, hobby: dance}, {name:bob, age: 27, hobby: cars}, {name: chloe, age:45, hobby: painting}];
    
    let result = array.map(i => i.name)
    
    Login or Signup to reply.
  2. I don’t quite understand what you mean.

    your origin array

    array = [{name: alice, age: 18, hobby: dance}, {name:bob, age: 27, hobby: cars}, {name: chloe, age:45, hobby: painting}];

    the new array you want get

    new array = [alice, bob, chloe];

    you can use

    array.map(o => ({ [o.name]: o }))
    

    Is this the answer you want?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search