[英]JavaScript - Get the value of a variable generated inside a forEach loop
I would like to get the value of a variable generated inside a forEach loop outside of it, please look at the code snippet: 我想获取在forEach循环之外生成的变量的值,请查看代码段:
var variable = "";
array.forEach(el=>{
variable = el.checkStatus;
})
//How to get the value of variable here ?
var statuses = array.map(el => {
return el.checkStatus;
})
Then simply call statuses[index] depending on the status you want 然后根据您想要的状态简单地调用statuss [index]
You could make variable
an array of each elements checkStatus
values. 您可以使
variable
成为每个元素的checkStatus
值的数组。
var variable = new Array();
array.forEach(el => {
variable.push(el.checkStatus());
})
This would be more elegantly done using an array map
function though. 不过,使用数组
map
函数可以更优雅地完成此操作。
var variable = array.map(el => el.checkStatus())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.