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

Need to create a new array from old one with some changes

I have this array:

let oldarr = [{typeid:1,percent:50,key:1, name:"ger"},
              {typeid:3,percent:50,key:2, name:"geghr"},
              {{typeid:5,percent:50,key:3, name:"ggdfer"}]

I want to create a new one that looks like this:

let newarr = [{id:'', percent:''}]

The newarr‘s id is equal to typeid of oldarr. How to create new array?

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 could achieve this using the map function:

let oldarr = [{typeid:1,percent:50,key:1, name:"ger"},
              {typeid:3,percent:50,key:2, name:"geghr"},
              {typeid:5,percent:50,key:3, name:"ggdfer"}];

let newArray = oldarr.map(item=> ({id : item.typeid, percent: item.percent}));

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