【发布时间】:2017-08-19 18:13:01
【问题描述】:
我目前尝试使用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。
任何人都知道它为什么这样做并且知道我该如何解决它?
【问题讨论】:
标签: javascript undefined push