Javascript 如何更好地保存数组中所有对象的子对象?
所以我在Vue中创建了一个项目,我想迭代所有的['monitors',并保存不同的部分。我怎样才能做到最好? 谢谢Javascript 如何更好地保存数组中所有对象的子对象?,javascript,arrays,loops,vue.js,object,Javascript,Arrays,Loops,Vue.js,Object,所以我在Vue中创建了一个项目,我想迭代所有的['monitors',并保存不同的部分。我怎样才能做到最好? 谢谢 listOfAllTrips=this.wlData['monitors'][0]。行[0]。出发; listOfAllTrips2=this.wlData['monitors'][1]。行[0]。偏差; listOfAllTrips3=this.wlData['monitors'][2]。行[0]。偏差; 这里是一个使用Ecma脚本数组映射函数的迭代,该函数将处理所有tri
listOfAllTrips=this.wlData['monitors'][0]。行[0]。出发;
listOfAllTrips2=this.wlData['monitors'][1]。行[0]。偏差;
listOfAllTrips3=this.wlData['monitors'][2]。行[0]。偏差;
这里是一个使用Ecma脚本数组映射函数的迭代,该函数将处理所有trip的列表超过正式硬编码的3个时的情况
现在要访问每个出发点,您只需提供访问索引,例如
listOfAllTrips[1]
这将返回第二次行程,因为数组索引从0开始。您可以使用函数
map
和分解分配,如下所示:
设m=[{lines:[{departments:[“Ele”,“Stack”]}],{lines:[{departments:[“Ele2”,“Stack2”]}]}];
让result=m.map({lines:[偏离]})=>偏离);
控制台日志(结果)代码>listOfAllTrips=this.wlData['monitors'].map(monitor=>monitor.lines[0]。出发)代码>非常感谢ritaj,这正是我想要的!:-)
listOfAllTrips[1]