[英]Array element are undefined Javascript
The array elements in Bind are undefined even though there is data in bindInfo. 即使bindInfo中有数据,Bind中的数组元素也是未定义的。 Any suggestions.
有什么建议么。
let bindinfo =
{
clientid: 1,
clientname: 'Web Client',
nowutc: now_utc,
bindlist: Bindings(this.props.bindDetails)
}
Bindings(bin)
{
var Bind = [];
Bind = bin.map(bindItem => {
var bindInfo;
bindInfo = {
bindingid: bindItem.bindId,
function: bindItem.functionName,
aggregation: bindItem.aggregation,
datalimit: bindItem.datalimit
}
Bind.push(bindInfo);
});
return Bind;
}
After the .map
operation completes, bind
is assigned to the result of the .map
- that is, the element returned on each iteration of .map
is the new array elemenet. .map
操作完成后, bind
分配给.map
的结果-也就是说, .map
每次迭代返回的元素是新数组elemenet。 But you never return anything from the mapper function, so even if you initially push
to Bind
, your later reassignment of Bind
resets it. 但是,您永远不会从mapper函数返回任何内容,因此,即使您最初
push
Bind
,以后重新分配 Bind
也会重置它。
Either use .map
properly and return the item on each iteration: 正确使用
.map
并在每次迭代中返回该项目:
const Bind = bin.map(bindItem => {
return {
bindingid: bindItem.bindId,
function: bindItem.functionName,
aggregation: bindItem.aggregation,
datalimit: bindItem.datalimit
};
});
Or (not recommended) use forEach
rather than .map
, and don't reassign Bind
: 或者(不建议)使用
forEach
而不是.map
,并且不要重新分配Bind
:
const Bind = [];
bin.forEach(bindItem => {
var bindInfo = {
bindingid: bindItem.bindId,
function: bindItem.functionName,
aggregation: bindItem.aggregation,
datalimit: bindItem.datalimit
}
Bind.push(bindInfo);
});
Use .map
when you want to transform one array into another through the result of the call of .map
. 使用
.map
当你想通过调用的结果一个数组转换为另一种.map
。 Otherwise, for generic looping, use forEach
. 否则,对于通用循环,请使用
forEach
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.