3 回答
TA贡献1998条经验 获得超6个赞
这不完全是这样的filter。filter的回调函数应返回一个布尔结果,该结果确定传入回调的元素是否应包含在 的返回值中filter。执行过滤后,我们将map每个结果对象提取其users属性。
const /*this.state.*/data = {"accounts": [{"ID": "001yQdmAAE","email": "inhome.user@ator.com","users": [{"Name": "Inhome User","MobilePhone": "34877"}]},{"ID": "00mAAE","email": "in.user@ator.com","users": [{"Name": "Inhome r","MobilePhone": "300077"}]}]};
const targetID = "00mAAE";
const result = data.accounts
.filter(e => e.ID === targetID)
.map(e => e.users);
console.log(result);
但如果IDs 是唯一的,则使用find代替filter,它只定位第一个匹配项:
const data = {"accounts": [{"ID": "001yQdmAAE","email": "inhome.user@ator.com","users": [{"Name": "Inhome User","MobilePhone": "34877"}]},{"ID": "00mAAE","email": "in.user@ator.com","users": [{"Name": "Inhome r","MobilePhone": "300077"}]}]};
const targetID = "00mAAE";
const result = data.accounts.find(e => e.ID === targetID);
if (result) {
console.log(result.users);
}
TA贡献2016条经验 获得超9个赞
过滤器返回 true 或 false,如果它返回 false 则该元素被过滤掉。
您应该首先过滤返回 true 或 false,然后使用 map 仅获取用户,然后使用 flat 将二维数组转换为普通数组。
const res=this.state.data.filter(
res=>
//return true or false in filter
res.AccountID==record.AccountID
).map(
//only need the users property
res=>res.users
).flat();//flatten [users] to users
console.log(res);
添加回答
举报