Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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 未捕获的TypeError:无法设置未定义的属性“0”_Javascript_Jquery_Loops_Each - Fatal编程技术网

Javascript 未捕获的TypeError:无法设置未定义的属性“0”

Javascript 未捕获的TypeError:无法设置未定义的属性“0”,javascript,jquery,loops,each,Javascript,Jquery,Loops,Each,我不断得到未捕获的TypeError:无法设置未定义的属性“0”,但我无法终生找出错误所在 var anchor = []; getAnchors(); function getAnchors() { $('.anchor').each(function(i) { anchor[i] = $(this).offset().left; }); } 那么这段代码有什么问题呢? 我已将锚点声明为数组。 我已经检查了一个小时了。 错误在于我。但是错误是什么呢 编辑: 是

我不断得到未捕获的TypeError:无法设置未定义的属性“0”,但我无法终生找出错误所在

var anchor = [];
getAnchors();
function getAnchors() {
    $('.anchor').each(function(i) {
        anchor[i] = $(this).offset().left;
    });
}
那么这段代码有什么问题呢? 我已将锚点声明为数组。 我已经检查了一个小时了。 错误在于我。但是错误是什么呢

编辑:
是否尝试返回数组?像这样:

function getAnchors() {
    var allAnchors = [];
    $('.anchor').each(function(i) {
        allAnchors.push($(this).offset().left);
    });
    return allAnchors;
}

var anchor = getAnchors();

是否尝试返回数组?像这样:

function getAnchors() {
    var allAnchors = [];
    $('.anchor').each(function(i) {
        allAnchors.push($(this).offset().left);
    });
    return allAnchors;
}

var anchor = getAnchors();

需要先创建对象特性,然后才能为其指定值

    var anchor = [];
    $('.anchor').each(function(i) {
        anchor[i] = [{
            pos: null,
            id: null
        }];
        anchor[i]['pos'] = $(this).offset().left;
        anchor[i]['id'] = $(this).attr('id');
    });

然后进行赋值并用值覆盖null。

在为对象赋值之前,需要先创建对象属性

    var anchor = [];
    $('.anchor').each(function(i) {
        anchor[i] = [{
            pos: null,
            id: null
        }];
        anchor[i]['pos'] = $(this).offset().left;
        anchor[i]['id'] = $(this).attr('id');
    });
然后进行赋值并用值覆盖null。

您可以使用$.map构建数组:

var anchor;
function getAnchors() {
    anchor = $.map($('.anchor'), function(el) { return $(el).offset().left; });
}
您可以使用$.map构建阵列:

var anchor;
function getAnchors() {
    anchor = $.map($('.anchor'), function(el) { return $(el).offset().left; });
}
您可以这样做:

var anchors = $('.anchor').map(function() {
  return {
    pos: $(this).offset().left,
    id: this.id
  }
}).get();
您可以这样做:

var anchors = $('.anchor').map(function() {
  return {
    pos: $(this).offset().left,
    id: this.id
  }
}).get();

我找到了答案。我只需要在函数中声明锚点

getAnchors();
function getAnchors() {
    var anchor = [];
    $('.anchor').each(function(i) {
        anchor[i] = $(this).offset().left;
    });
    // perhaps sort anchor
    console.log(anchor);
}

谢谢你的回复。

我找到了答案。我只需要在函数中声明锚点

getAnchors();
function getAnchors() {
    var anchor = [];
    $('.anchor').each(function(i) {
        anchor[i] = $(this).offset().left;
    });
    // perhaps sort anchor
    console.log(anchor);
}

感谢您的回复。

我注意到的第一个问题是,您正在为数组分配非索引键。你是说anchor[i]={}吗?数组没有这样的键,你在寻找一个Object1问题,我注意到你在给数组分配非索引键。你是说anchor[i]={}?数组没有这样的键,你在寻找一个对象,这样做毫无意义。检查@adaneo的答案,如果你需要的话。这样做毫无意义。检查@adaneo的答案,如果这是你需要的。