Object 使用映射中的键名访问对象属性
我有下面的对象数组Object 使用映射中的键名访问对象属性,object,ecmascript-6,Object,Ecmascript 6,我有下面的对象数组 const reports = [{id:3, name:'three', description:'three d', other: 'other 3'}, {id:2, name:'two', description:'two d', other: 'other 2'}]; 我只想过滤掉每个对象的2个属性,下面是我想要的输出 [{id:3, name:'three'}, {id:2, name:'two'}]; 所以我试着这样做 const reportList =
const reports = [{id:3, name:'three', description:'three d', other: 'other 3'}, {id:2, name:'two', description:'two d', other: 'other 2'}];
我只想过滤掉每个对象的2个属性,下面是我想要的输出
[{id:3, name:'three'}, {id:2, name:'two'}];
所以我试着这样做
const reportList = reports.map((report) => {id,name} );
console.log(reportList);
抛出错误
ReferenceError:未定义id
甚至我也可以通过使用这种方法来实现这一点
this.reportList = reports.map((report) => ({
id: report.id,
name: report.name,
description: report.description
}));
但在这里我需要编写额外的代码,我想使用对象访问器使用key,我能实现吗?必须将返回的对象文本括在括号中。否则,将考虑使用大括号表示函数体。以下工作:
const报告=[{
id:3,
姓名:"三",,
描述:“三个d”,
其他:“其他3”
}, {
id:2,
姓名:"两",,
描述:“两个d”,
其他:“其他2”
}];
const reportList=reports.map({id,name})=>({
身份证件
名称
}));
console.log(reportList)代码>谢谢。现在明白了。