Javascript Push() - 未定义的输出

标签 javascript undefined push

我目前尝试使用 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/

相关文章:

javascript - Javascript 中的 array.push 不起作用

javascript - 比较数字与javascript中的数组

c# - 查找aspx页面中存在的所有控件和子控件id以及相应的ascx用户控件

javascript - Mongodb $in 以与数组中相同的顺序获取结果

php - 查询结果在html中的布局

javascript - 为什么在React中未定义此数组?

javascript - React OIDC-Client IE Promise 未定义

node.js - id 未定义(sails.socket.emit)

git - 我更新了来自上游/master 的 pull 请求的分支。如果我再次将分支推送到github,会污染 pull 请求吗?

iphone - iOS - 在没有实际推送的情况下显示推送弹出窗口并播放推送声音