gpt4 book ai didi

Javascript数组父子结构

转载 作者:行者123 更新时间:2023-12-01 03:22:38 25 4
gpt4 key购买 nike

我正在尝试将 Javascript 数组加载到 Select2(复选框的 jQuery 替代品)上,如下所示:

$('select').select2({
data: [
{
id: 'value',
text: 'Text to display'
},
// ... more data objects ...
]
});

这是引用:https://select2.github.io/options.html#data

我的数据结构如下:

data: 
[
{
id: "Cheek",
text: "Cheek",
children: [{id: "Cheek Palettes", text: "Cheek Palettes"},
{id: "Blush", text: "Blush"},
{id: "Bronzer", text: "Bronzer"},
{id: "Contour", text: "Contour"},
{id: "Highlighter", text:"Highlighter"}],
id: "Eye",
text: "Eye",
children: [{id: "Eye Palettes", text: "Eye Palettes"},
{id: "Mascara", text: "Mascara"},
{id: "Eyeliner", text: "Eyeliner"},
}
]

我创建的选择框仅显示最后一个外群“Eye”及其子数据。我在这里做错了什么?

最佳答案

您的数据结构略有偏差:

data: 
[
{
id: "Cheek",
text: "Cheek",
children: [{id: "Cheek Palettes", text: "Cheek Palettes"},
{id: "Blush", text: "Blush"},
{id: "Bronzer", text: "Bronzer"},
{id: "Contour", text: "Contour"},
{id: "Highlighter", text:"Highlighter"}]
}, // <--- This makes this an array of JSON objects
{
id: "Eye",
text: "Eye",
children: [{id: "Eye Palettes", text: "Eye Palettes"},
{id: "Mascara", text: "Mascara"},
{id: "Eyeliner", text: "Eyeliner"},
} // <--- Whereas before, you were replacing the id "Cheek" with "Eye" because JSON only allows one value per key
}
]

关于Javascript数组父子结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45069684/

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