Javascript循环并构建数组,但变量索引未定义

发布于 2024-12-04 19:37:36 字数 363 浏览 1 评论 0原文

不要笑,以为我有一整天工作的金发时刻,因为我有点不习惯 JS。任何帮助都对我认为是一个愚蠢的简单问题表示赞赏。

optionarray = [];

for(i=0;i<response.length;i++) {

    optionarray[i]['content'] = response[i]['name'];
    optionarray[i]['value'] = response[i]['id'];
}

当尝试将其添加到数组并构建它时,我不断收到 optionarray[i] is undefined 的信息。我知道我做了一些可笑的愚蠢的事情,我只是不记得是什么:)

非常感谢您提前提供的任何帮助。

dont laugh think im having a long day work blonde moment as im a bit out of practice with JS. Any helped appreciated for what I think is a stupidly simple problem.

optionarray = [];

for(i=0;i<response.length;i++) {

    optionarray[i]['content'] = response[i]['name'];
    optionarray[i]['value'] = response[i]['id'];
}

I keep getting optionarray[i] is undefined when trying to add it to the array and build it. I know im doing something ridiculously stupid I just can't remember what :)

Many thanks for any help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沒落の蓅哖 2024-12-11 19:37:36
optionarray = [];

for(i=0;i<response.length;i++) {

    optionarray[i] = {
            'content' :response[i]['name'], 
            'value': response[i]['id']
    };

}
optionarray = [];

for(i=0;i<response.length;i++) {

    optionarray[i] = {
            'content' :response[i]['name'], 
            'value': response[i]['id']
    };

}
蘑菇王子 2024-12-11 19:37:36

您正在尝试访问 optionarray[i] 的属性,该属性不存在。

您在每次迭代中应该做的是

  1. 将一个新对象添加到 optionarray
  2. 设置该对象的属性

您可以同时执行这两个操作,如下所示:

optionarray = [];

for(i=0;i<response.length;i++) {
    optionarray.push({
        content: response[i]['name'],
        value: response[i]['id']
    });
}

You are trying to access properties of optionarray[i], which does not exist.

What you should be doing in each iteration is

  1. adding a new object to optionarray
  2. setting that object's properties

You can do both at once like this:

optionarray = [];

for(i=0;i<response.length;i++) {
    optionarray.push({
        content: response[i]['name'],
        value: response[i]['id']
    });
}
念三年u 2024-12-11 19:37:36

我认为你只需要在 for 循环中初始化 optionarray[i] 对象:

var optionarray = []; //NOTE: I added var here so because otherwise it's an implicit global

for(i=0;i<response.length;i++) {
    optionarray[i] = {};
    optionarray[i]['content'] = response[i]['name'];
    optionarray[i]['value'] = response[i]['id'];
    // BETTER: optionarray.push({content: response[i]['name'], value: response[i]['id']});
}

如果我没有记错的话。

I think you just need to initialize the optionarray[i] object within your for loop:

var optionarray = []; //NOTE: I added var here so because otherwise it's an implicit global

for(i=0;i<response.length;i++) {
    optionarray[i] = {};
    optionarray[i]['content'] = response[i]['name'];
    optionarray[i]['value'] = response[i]['id'];
    // BETTER: optionarray.push({content: response[i]['name'], value: response[i]['id']});
}

if I'm not mistaken.

鱼窥荷 2024-12-11 19:37:36

试试这个

optionarray[i] = [];
optionarray[i]['content'] = response[i]['name'];     
optionarray[i]['value'] = response[i]['id']; 

你需要首先将 optionarray[i] 定义为数组

Try this one

optionarray[i] = [];
optionarray[i]['content'] = response[i]['name'];     
optionarray[i]['value'] = response[i]['id']; 

You need to define optionarray[i] as array first

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文