gpt4 book ai didi

javascript - 我如何重构这个 JSON 以使其更易于访问

转载 作者:行者123 更新时间:2023-11-28 21:03:43 25 4
gpt4 key购买 nike

我有一些 JSON 数据,格式如下:

 [ {"stage1" : [{
"checkpoints" :
[
{
"id" : "checkpoint1",
"name" : "a checkpoint name 1",
"purpose" : "a string about the purpose here",
},
{
"id" : "checkpoint2",
"name" : "a checkpoint name 2",
"purpose" : "a string about the purpose here",

}
],

"stages" :
[
{

"id" : "an id here",
"name" : "a checkpoint name 1",
"purpose" : "a string about the purpose here yah",

}
]
}
]},

{"stage2" : [{
"checkpoints" :
[
{
"id" : "checkpoint1",
"name" : "a checkpoint name 1",
"purpose" : "a string about the purpose here",
},
{
"id" : "checkpoint2",
"name" : "a checkpoint name 2",
"purpose" : "a string about the purpose here",

}
],

"stages" :
[
{

"id" : "an id here",
"name" : "a checkpoint name 1",
"purpose" : "a string about the purpose here yah",

}
]
}
]},

{"stage3" : [{
"checkpoints" :
[
{
"id" : "checkpoint1",
"name" : "a checkpoint name 1",
"purpose" : "a string about the purpose here",
},
{
"id" : "checkpoint2",
"name" : "a checkpoint name 2",
"purpose" : "a string about the purpose here",

}
],

"stages" :
[
{

"id" : "an id here",
"name" : "a checkpoint name 1",
"purpose" : "a string about the purpose here yah",

}
]
}
]},





]

目前我必须引用这样的数据才能达到以下阶段:

alert(data[0].stage1[0].checkpoints.length);  

要获取第 2 阶段的数据,我必须这样做:

alert(data[1].stage2[0].checkpoints.length); 

我想要做的只是使用阶段名称来访问数据,而不必在“数据”声明后指定索引:

alert(data.stagex[0].checkpoints.length); 

我不想在数据部分之后声明索引。如何重构 JSON,以便可以使用阶段名称来获取所需的数据,而无需先指定索引?

最佳答案

不要将 Stage1、Stage2 放入数组中。它们可以作为 key 直接访问

关于javascript - 我如何重构这个 JSON 以使其更易于访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10381735/

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