Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pluck key value from object of array

i have object of array like this:

var arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}];

i want output like this :

[{id:1}, {id:2}]

Thanks in advance

like image 630
Umar Tariq Avatar asked Nov 02 '25 07:11

Umar Tariq


1 Answers

Just use Array#map

const arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}];
const mapped = arr.map(({id}) => ({ id }));

console.log(mapped);

In simple form

const arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}];
const mapped = arr.map(item => { return { id: item.id };});

console.log(mapped);
like image 80
Suren Srapyan Avatar answered Nov 03 '25 20:11

Suren Srapyan