我想显示数组,但只显示姓名和年龄
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);Run Code Online (Sandbox Code Playgroud)
没用。我错过了某个地方的回报,对吧?
您应该在每次迭代中使用 包装对象语句()。
另外,我更喜欢使用解构赋值:
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);Run Code Online (Sandbox Code Playgroud)