我目前尝试使用 push()
函数在数组中保护不同用户的数据。
这是我当前的代码:
function data()
{
var information = [];
var imgs = '';
var percentage;
var imgs = 'ABC';
var percentage = '1';
information.push({ imgs: imgs, chance: percentage });
var imgs = 'DEF';
var percentage = '2';
information.push({ imgs: imgs, chance: percentage });
console.log(information);
information.forEach(function(deposit)
{
var deposit=deposit.imgs;
var chance=deposit.chance;
console.log(deposit);
console.log(chance);
});
}
这是console.log(information)
的输出:
[ { imgs: 'ABC', chance: '1' }, { imgs: 'DEF', chance: '2' } ]
这是 information.forEach(function(deposit)
的输出:
ABC
undefined
DEF
undefined
这就是我的问题。
正如您所看到的,它输出的机会为 undefined
但它应该输出 1 和 2。
有人知道为什么会这样并且知道我该如何解决它吗?
最佳答案
在下一行中,您重新分配存款对象:
var deposit=deposit.imgs;
只需更改此变量名称即可。或者在存款前分配机会。
关于Javascript Push() - 未定义的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45774885/