gpt4 book ai didi

javascript - 如何从嵌套数组中获取字段值?

转载 作者:行者123 更新时间:2023-12-03 00:06:15 24 4
gpt4 key购买 nike

我需要获取值和轴。这是我所拥有的。

var d = [
[
{axis:"Customer Service",value:0.6},
{axis:"Data",value:0.6},
{axis:"Network",value:0.6},
{axis:"Product",value:0.6},
{axis:"Specialized",value:0.6},
{axis:"Threat",value:0.6},
],
[
{axis:"Customer Service",value:0.3},
{axis:"Data",value:0.3},
{axis:"Network",value:0.3},
{axis:"Product",value:0.3},
{axis:"Specialized",value:0.3},
{axis:"Threat",value:0.3},
]

function myFunction() {
var sample = d[0];
alert(sample);

有什么办法可以得到输出客户服务:0.6

最佳答案

当查看复合数据结构时,方括号将表示数组和大括号对象。在本例中,有一个包含对象的数组的数组,并且需要通过它们的“轴”来标识对象。

var d = [
[
{axis:"Customer Service",value:0.6},
{axis:"Data",value:0.6},
{axis:"Network",value:0.6},
{axis:"Product",value:0.6},
{axis:"Specialized",value:0.6},
{axis:"Threat",value:0.6},
],
[
{axis:"Customer Service",value:0.3},
{axis:"Data",value:0.3},
{axis:"Network",value:0.3},
{axis:"Product",value:0.3},
{axis:"Specialized",value:0.3},
{axis:"Threat",value:0.3},
]
];

function myFunction() {
let sample = '';
let index = 0; // which of the array items is being looked at, if in a loop
// this value could be incremented to get the Customer Service value from
// all of the entries in the outer array.
d[index].forEach(function(dItem) { // loop through the array of a single object
// looping through allows you to access any of the axis objects, not just the first
if (dItem.axis === 'Customer Service') { // check which object
sample = dItem.value; // if the desired object then set sample
}
});
// alternatively check to see that sample is still not equal to '' before
// alerting
alert(sample);
}
myFunction();

关于javascript - 如何从嵌套数组中获取字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54952929/

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