Reputation: 2422
I want to display the array but only with name and age
const users = [{name: 'john', age: 20, instrument: 'guitar'}, {name: 'mary', age: 20, instrument: 'piano'}];
let userList = users.map(users => {name: users.name, users.instrument })
console.log(userList);
didn't work. I'm missing a return somewhere right?
Upvotes: 2
Views: 3298
Reputation: 68933
You should wrap the object statement in each iteration with ()
.
Also, I prefer using Destructuring assignment:
const users = [{name: 'john', age: 20, instrument: 'guitar'}, {name: 'mary', age: 20, instrument: 'piano'}];
var new_users = users.map(({name,instrument}) => ({name, instrument}));
console.log(new_users);
Upvotes: 5
Reputation: 72
=
when setting users
.users
but use user
.'
after 'guitar
()
around the object in the mapping function as it will be treated as arrow function if forgottenIn the end it should look like this:
const users = [{name: 'john', age: 20, instrument: 'guitar'}, {name: 'mary', age: 20, instrument: 'piano'}];
const mapped = users.map(user => ({name: user.name, instrument: user.instrument}));
console.log(mapped);
Upvotes: 1
Reputation: 3305
You just need to wrap object inside ()
const users = [{name: 'john', age: 20, instrument: 'guitar'}, {name: 'mary', age: 20, instrument: 'piano'}];
var result = users.map(user => ({ name: user.name, instrument: user.instrument }));
console.log(result)
Upvotes: 3