【发布时间】:2019-01-30 19:19:45
【问题描述】:
我正在尝试向我的单位数组添加一个属性(分数)。 Scores 是一个对象数组,其中包含一个带有单位的公共 id。
this.units.map(unit => {
unit.score = scores
.filter(score => unit.id === score.unit_id);
});
数组中包含的对象。
scores
{
unit_id: 1234,
score: 12,
}
this.units
[
{
id: 1234,
type: 'maths',
}
{
id: '5678',
type: 'maths',
}
]
看起来不错。是不是因为unit.id在过滤器中不可用?
【问题讨论】:
-
现在您已经编辑了问题中的代码,所以它可以工作了,那么问题是什么?
标签: javascript arrays filter mapping