小编Luk*_*yyy的帖子

Javascript 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);
    });


}
Run Code Online (Sandbox Code Playgroud)

这是输出console.log(information):

[ { imgs: 'ABC', chance: '1' }, { imgs: 'DEF', chance: '2' } ]
Run Code Online (Sandbox Code Playgroud)

这是输出information.forEach(function(deposit):

ABC
undefined
DEF
undefined …
Run Code Online (Sandbox Code Playgroud)

javascript push undefined

0
推荐指数
1
解决办法
232
查看次数

标签 统计

javascript ×1

push ×1

undefined ×1