Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebug在数组中显示大量未定义的条目_Javascript_Jquery_Arrays_Firebug - Fatal编程技术网

Javascript Firebug在数组中显示大量未定义的条目

Javascript Firebug在数组中显示大量未定义的条目,javascript,jquery,arrays,firebug,Javascript,Jquery,Arrays,Firebug,我正在定制。但我遇到了一个问题。一个快速的console.log(选项)显示数组的结果: [未定义,未定义,“赛琳娜·戈麦斯”,“米拉·库尼斯”,未定义,未定义,未定义,未定义,未定义,未定义,未定义,“麦莉·赛勒斯”] 当我发出警报时(options.length)结果是12。事实上,选择中有4个选项 生成数组的代码: var options = new Array; $(this).children('option').each(function () {

我正在定制
。但我遇到了一个问题。一个快速的
console.log(选项)显示数组的结果:

[未定义,未定义,“赛琳娜·戈麦斯”,“米拉·库尼斯”,未定义,未定义,未定义,未定义,未定义,未定义,未定义,“麦莉·赛勒斯”]

当我发出
警报时(options.length)结果是12。事实上,选择中有4个选项

生成数组的代码:

var options = new Array;               
$(this).children('option').each(function () {
    if ($(this).val()) {
        options[$(this).val()] = $(this).text();
    }
});
console.log(options);
我不知道是什么问题。。我添加了
if($(this).val())
,以确保数组中没有静态数据,但仍然存在

注意:但是,当我在firebug中打开数组时,它只显示正确的条目。

$(this).val()
不能是0,1,2,3,而是2,3,11(第四个在哪里?)

使用
options.push($(this.text())或将其用作对象,以避免自动生成缺少的索引

另外,
$(this).val()
将计算为
false
如果它是空的
0
,那可能是第四个吗?

我猜
$(this).val()
对于
$(this).text()
“赛琳娜·戈麦斯”是数字2,“麦莉·赛勒斯”是11。请尝试以下方法:

options.push($(this).text()); // ensure that your array is filled as an array 
例如,这在javascript中是可以的

var a = [];
a[4] = 'test';
a["something"] = this;
并显示console.log

[undefined, undefined, undefined, undefined, 'test']
试试这个

var options = [];               
$(this).children('option').each(function () {
    if ($(this).val()) {
        options.push($(this).text());
    }
});
console.log(options.join(","));
如果需要文本键,则使用javascript对象作为关联数组

var options = {};               
$(this).children('option').each(function () {
    if ($(this).val()) {
        options[$(this).val()] = $(this).text();
    }
});
$.each(options, function(key, value) { 
    console.log(key + ': ' + value); 
});

这里有一些误导性的回答:

  • Javascript数组索引是字符串。数组只是具有特殊长度属性和一些方便方法的对象,但在其他方面它们只是对象。它们的属性是字符串。数值属性将被视为索引,并用于设置长度属性

  • 没有“丢失的钥匙”。如果创建稀疏数组,其长度将设置为最高索引+1。如果对从0到长度-1的所有索引进行迭代,则任何不存在的索引都将返回undefined

  • e、 g

    仅仅因为Firebug将IDEX 1到IDEX 3(包括IDEX 1到IDEX 3)列为未定义,并不意味着它们存在

    OP的简单循环版本为:

    var opts = this.options;
    for (var i=0, iLen=opts.length; i<iLen; i++) {
      if (opts[i].value) {
        options[opts[i].value] = opts[i].text;
      }
    }
    
    var opts=this.options;
    
    对于(var i=0,iLen=opts.length;iOf-course。这很有道理。我更喜欢php,不知道JS的基本知识。谢谢,修复了。谢谢你的快速回复。Frits打败了你,但希望+1能让你开心:)这确实很好,但我需要数组中的键。在基于js的数组中是否允许使用基于文本的键?@Kalle,允许,是的,但您必须
    $。每个
    for(x in options)
    循环通过
    options
    获取基于键的数组中的项,以避免未定义的项items@Kalle:这就是你想要的吗?@Kalle:这是
    var options=new Object()的简写符号末尾有一个未定义的条目?你能在最后一次迭代时显示准确的日志吗?哦,实际上我的错。有一个旧警报();这里:$Thank man,离目标更近了一步。@Kalle,那么你需要更明确地表达你的if…
    if($(this.val()!==“”)
    if($(this.val().length)
    你可以使用
    []
    而不是
    新数组
    -看起来更漂亮。不。它们仅允许在对象中使用。PHP中没有区别,但在JavaScript中它非常重要。@Frits van Campen-JavaScript中的所有键都是字符串,它们从来不是其他任何东西。数组只是具有特殊长度属性的对象和一些方便的方法,否则它们只是对象。@RobG不是这样。数组与对象非常不同。你有证据支持你的主张吗?
    var opts = this.options;
    for (var i=0, iLen=opts.length; i<iLen; i++) {
      if (opts[i].value) {
        options[opts[i].value] = opts[i].text;
      }
    }