-1

What I have is this:

const objArr = [
    {name: "John", id: 1}, 
    {name: "Marry", id: 2}, 
    {name: "Jack", id: 3}
  ] 

And I want this:

const names = [
    "John",
    "Marry",
    "Jack"
  ]

How? Thanks!

3
  • Have you made any attempt at all yourself yet? Commented Nov 19, 2018 at 11:15
  • Clearly he asked question without even trying himself Commented Nov 19, 2018 at 11:18
  • There's no need for that, I am fetching some objects from server, doing this for hours and lost my self... Actually the first answer is correct and I already tried that but did not realize that it work... No need for minuses, I am just tired... Thanks guys Commented Nov 19, 2018 at 11:22

3 Answers 3

2

Use Array.prototype.map() to return only the name property.

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

const objArr = [
    {name: "John", id: 1}, 
    {name: "Marry", id: 2}, 
    {name: "Jack", id: 3}
  ] 

const names = objArr.map(p => p.name);

console.log(names);

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

1 Comment

You should really have voted to close as duplicate
1

here you go ;)

const names = objArr.map(person => person.name);

Comments

1

Just use map method in combination with destructuring by passing a callback provided function as argument.

const objArr = [{name: "John", id: 1}, {name: "Marry", id: 2}, {name: "Jack", id: 3}] 
console.log(objArr.map(({name}) => name));

1 Comment

You should really have voted to close as duplicate

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.