gpt4 book ai didi

javascript - 在对象的现有循环内循环

转载 作者:行者123 更新时间:2023-11-28 17:23:14 26 4
gpt4 key购买 nike

我当前正在 JavaScript 中循环一个对象,并成功从 _source 级别获取了我需要的内容。我现在的问题是 _source 内的 color 下还有另一个数组。

目前,console.log(searchResult); 给了我这个对象:

1
_source
color
1
color_type
type
name
id

我可以访问 typecolor,因为它们位于 _source 级别,但我需要访问 color_typecolor 中另一个数组的一部分,它显然需要是我最初索引元素中包含的信息。

我需要在此处创建一个新循环来访问颜色信息吗?

    let searchResult = response.hits.hits;
console.log(searchResult);
for(let i = 0; i < searchResult.length; i++) {

//This line displays the name.id properly
document.getElementById("name").value = searchResult[i]._source.type.name.id;

//this line gives undefined
document.getElementById("color").value = searchResult[i]._source.color[i].color_type;
})
}

最佳答案

由于您只对第二个元素(索引 1)感兴趣,因此这应该可以解决问题:

document.getElementById("color").value = searchResult[i]._source.color[1].color_type;

关于javascript - 在对象的现有循环内循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52082384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com