为什么新的 javascript 数组有“未定义"条目? [英] Why does a new javascript array have 'undefined' entries?

查看:22
本文介绍了为什么新的 javascript 数组有“未定义"条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的示例演示了一个数组,当您填充它时,它也会在其中获取所有类型的未定义条目.

Here's a sample that demonstrates an array that as you fill it in, it gets all types of undefined entries in it as well.

这是在 firefox 19.0/firebug 上,不确定在其他浏览器上是否会发生.

This is on firefox 19.0/firebug, not sure if it happens on other browsers.

基本流程:

  1. 对象已初始化(最底部)
  2. 它调用加载"
  3. 当 ajax 在加载中返回时,data.objects 包含一个 json 对象数组.该数组上没有未定义的条目.
  4. setObjects 被调用,其中来自 ajax 调用的所有对象都被复制到 this.objects.
  5. 当它们被复制时,我可以看到 undefined 条目显示在 firebug 中,如果我没有第二次传递数组并拼接未定义的条目,它会在尝试访问元素时破坏胡须模板中的数组.
  1. Object is initialized (very bottom)
  2. It calls "load"
  3. When the ajax returns in load, data.objects contains an array of json objects. This array has no undefined entries on it.
  4. setObjects gets called where all the objects from the ajax call are copied to this.objects.
  5. As they are copied I can see undefined entries show up in firebug, If I didn't pass the array a second time and splice out the undefined entries it breaks mustache when trying to access the elements of the array in a template.

为什么 javascript 会自动用 undefined 条目填充 this.objects 数组?

Why does javascript automatically pad out the this.objects array with undefined entries?

代码如下:

function MailerFromProfile( )
{
    // privileged
    this.objects = [];
    this.load( );
}

MailerFromProfile.prototype.setObjects = function( objects )
{
    for( var i in objects )
    {
        if( 'undefined' !== objects[ i ] )
        {
            this.objects[ objects[ i ].id ] = objects[ i ];
        }
    }
    // I should not have to do this:
    for( var i = 0; i < this.objects.length; i++ )
    {
        if( typeof this.objects[ i ] === 'undefined' )
        {
            this.objects.splice( i, 1 );
            i--;
        }
    }
}

MailerFromProfile.prototype.setTemplate = function( n, v )
{
    this.template[ n ] = v;
}

MailerFromProfile.prototype.load = function( )
{
    jQuery.post(
        MAILER_PATH,
        { session: MAILER_SESSION,
          object : 'from_profile',
          action : 'list'
        },
        function( data )
        {
            if( typeof data.objects !== 'undefined' )
            {
                g_mailer_from_profiles.setObjects( data.objects );
            }
        },
        'json' );
}

var g_mailer_from_profiles = new MailerFromProfile( );

推荐答案

当你这样做时

this.objects[ objects[ i ].id ] = objects[ i ];

您要求数组将 this.objects 扩展到 objects[ i ].id.当您需要未填充索引处的元素时,引擎没有其他解决方案,只能为您提供 undefined.

you ask for the array to extend this.objects up to objects[ i ].id. There is no other solution for the engine than to give you undefined when you require the element at an unfilled index.

如果你的数组大部分是空的(一个稀疏数组),你可能应该使用一个对象作为映射,即用

If your array is mostly empty (a sparse array), you should probably use an object as map instead, that is initialize it with

this.objects = {};

这篇关于为什么新的 javascript 数组有“未定义"条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆