Reputation:
I am trying to make a list of value.
This is my objects:
var data = [
{
id: 40,
location: 'dhaka',
zip: 3244,
author: {name: 'jhon', age: 45}
},
{
id: 41,
location: 'nyc',
zip: 32444,
author: {name: 'doe', age: 45}
},
{
id: 46,
location: 'london',
zip: 3544,
author: {name: 'austin', age: 45}
},
]
var mylist = data.map(Object.values);
I am trying remove some specific key and do the list.
I am expecting output like these below:
[
[40, dhaka, jhon],
[41, nyc, doe],
[46, london, austin]
]
You may notice, i dont need zip and age value. I am expecting output exactly same as above.
Can anyone help me in this case?
Upvotes: 0
Views: 471
Reputation: 386560
You could destructure the object and take only the wanted parts for mapping.
var data = [{ id: 40, location: 'dhaka', zip: 3244, author: { name: 'jhon', age: 45 } },
{ id: 41, location: 'nyc', zip: 32444, author: { name: 'doe', age: 45 } },
{ id: 46, location: 'london', zip: 3544, author: { name: 'austin', age: 45 } }
],
result = data.map(({ id, location, author: { name } }) => [id, location, name]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 2
Reputation: 1534
You need to simply map()
properties of your objects to another array:
const data = [
{
id: 40,
location: 'dhaka',
zip: 3244,
author: {name: 'jhon', age: 45}
},
{
id: 41,
location: 'nyc',
zip: 32444,
author: {name: 'doe', age: 45}
},
{
id: 46,
location: 'london',
zip: 3544,
author: {name: 'austin', age: 45}
},
]
const results = data.map(d => [d.id, d.location, d.author.name]);
console.log(results);
Upvotes: 1