javascript - Construct an array of objects with two specific object value pair from another array of objects -


let's there array of object.

let source = [  {'name': 'a', age: '10', id: 'a0'},  {'name': 'b', age: '12', id: 'b2'},  {'name': 'c', age: '14', id: 'c4'},  {'name': 'd', age: '16', id: 'd6'},  {'name': 'e', age: '18', id: 'e8'} ] 

what trying achieve create new array consist specific 2 key-value pair. example result,

[  {'name': 'a', id: 'a0'},  {'name': 'b', id: 'b2'},  {'name': 'c', id: 'c4'},  {'name': 'd', id: 'd6'},  {'name': 'e', id: 'e8'} ] 

help appreciated.

you use destructuring assignment , array#map new array of new objects.

let source = [{ name: 'a', age: '10', id: 'a0' }, { name: 'b', age: '12', id: 'b2' }, { name: 'c', age: '14', id: 'c4' }, { name: 'd', age: '16', id: 'd6' }, { name: 'e', age: '18', id: 'e8' }],      result = source.map(({ name, id }) => ({ name, id }));    console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -