Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获取映射数组中的第一个索引?Js_Javascript_Loops - Fatal编程技术网

Javascript 如何获取映射数组中的第一个索引?Js

Javascript 如何获取映射数组中的第一个索引?Js,javascript,loops,Javascript,Loops,我需要得到数组和映射循环中的第一个索引 selectedValue: number; let test = this.indexOfSelectedExercise.sets.map((set, index) => { if(!set.completed) { this.selectedValue = set.values; } }) console.log('in' , test) } 我需要得到这个.sel

我需要得到数组和映射循环中的第一个索引

selectedValue: number;

   let test =  this.indexOfSelectedExercise.sets.map((set, index) => {
      if(!set.completed) {  
        this.selectedValue = set.values; 
      }
    })

    console.log('in' , test)
  }
我需要得到这个.selectedValue=set.values。 现在我得到最后一个索引,因为循环thought all数组并得到最后一项


set.values是示例4,5,6,7,8,我选择的值是8,但我需要是4,我认为您可能使用的地图不正确。也许.forEach()或简单地访问第一个索引会更容易些?也许我不明白这个问题

无论如何,要使
成为第一个值,而不是最后一个值,一个简单的方法是:

this.selectedValue=null;
让test=this.indexOfSelectedExercise.sets.map((set,index)=>{
如果(!set.completed&&!this.selectedValue){
this.selectedValue=set.values;
}
})
console.log(this.selectedValue)//应该是第一个值集
但如果你能更清楚地解释你想要做什么,我可能会帮上更多的忙。我真的认为
.map
对于这里的工作来说是错误的工具