我有这样的数据模型。
data: object = {
date: '11/11/18',
data: [3, 4, 7, 2, 5, 6, 8, 8]
}数据数组来自用户的一些输入。从指数0开始,我希望他们是职业,财务,personalGrowth,健康,家庭,人际关系,socialLife,态度。
我试图做的是得到最低的分数(这将是2‘健康’),并将它添加到这样的对象。
data: object = {
lowestScore = 2,
lowerScoreName = 'Health'
}我对javaScript相当陌生,我坚持要解决这个问题。谢谢
发布于 2018-11-30 07:16:54
您只需使两个相关数组(关联数组)如下所示:
data: object = {
dataTypes: ['Career', 'Finances', 'Personal Growth', 'Health', 'Family', 'Relationships', 'Social Life', 'Attitude'],
dataValues: [3, 4, 7, 2, 5, 6, 8, 8],
}然后得到最低的分数:
lowestScore: Math.min(this.dataValues),
lowestScoreIndex: this.dataValues.indexOf(this.lowestScore);并设置最低得分的名称:
lowestScoreName: this.dataTypes[lowestScoreIndex]你就这样吧!下面是完整的对象:
data: object = {
dataTypes: ['Career', 'Finances', 'Personal Growth', 'Health', 'Family', 'Relationships', 'Social Life', 'Attitude'],
dataValues: [3, 4, 7, 2, 5, 6, 8, 8],
lowestScore: Math.min(this.dataValues),
lowestScoreIndex: this.dataValues.indexOf(this.lowestScore);
lowestScoreName: this.dataTypes[lowestScoreIndex]
}https://stackoverflow.com/questions/53552143
复制相似问题