[英]How to get first index in map array? Js
我需要在数组和 map 循环中获取第一个索引。
selectedValue: number;
let test = this.indexOfSelectedExercise.sets.map((set, index) => {
if(!set.completed) {
this.selectedValue = set.values;
}
})
console.log('in' , test)
}
我需要得到 this.selectedValue = set.values。 现在我得到最后一个索引,因为循环考虑所有数组并得到最后一个项目。
set.values 是示例 4,5,6,7,8 而我的 selectedValue 是 8 但我需要是 4
我认为您可能错误地使用了 map。 也许.forEach() 或简单地访问第一个索引会更容易? 或者也许我不明白这个问题。
无论如何,使this.selectedValue
成为第一个值而不是最后一个值的简单方法是:
this.selectedValue = null;
let test = this.indexOfSelectedExercise.sets.map((set, index) => {
if(!set.completed && !this.selectedValue) {
this.selectedValue = set.values;
}
})
console.log(this.selectedValue) // Should be the first value set
但是,如果您更清楚地解释您想要做什么,我可能会提供更多帮助。 我真的认为.map
是适合这里工作的错误工具。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.