0

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?

2 Answers 2

2

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; }

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.