反映文件夹结构的 JSON 对象的路径列表
List of paths to JSON object that mirrors the folder structure
我正在构建一个基于小文件夹的 cms。我正在使用 php 提取文件夹的所有路径,我想使用 javascript 来构建网站。我知道我可以在 php 中做任何事情,但我在 javascript 中更流利,因此我想使用它而不是 php。无论如何 php 我得到这个列表:
0: {parent: "theater", child: "How_to_be_a_regisseur", text: "Project 1!↵Phasellus a quam non arcu accumsan rhon…ium leo ante, vitae volutpat diam consectetur in."}
1: {parent: "theater", child: "How_to_be_a_regisseur", img: "./content/theater/How_to_be_a_regisseur/img/download.jpeg"}
2: {parent: "theater", child: "How_to_be_a_regisseur", img: "./content/theater/How_to_be_a_regisseur/img/rsz_namibia_will_burrard_lucas_wwf_us_1.jpg"}
4: {parent: "theater", child: "WOOWW", text: "Project 1!↵Phasellus a quam non arcu accumsan rhon…ium leo ante, vitae volutpat diam consectetur in."}
...
12: {parent: "varia", child: "gggggg", text: "Project 1!↵Phasellus a
13: {parent: "varia", child: "gggggg", img: "./content/varia/gggggg/img/download.jpeg"}
14: {parent: "varia", child: "gggggg", img: "./content/varia/gggggg/img/rsz_namibia_will_burrard_lucas_wwf_us_1.jpg"}
...
20: {parent: "about", child: null, img: "./content/about/img/download.jpeg"}
parent
表示文件夹,child
表示子文件夹,img
或 text
表示该文件夹的内容。
有什么方法可以将此数组重组为具有此结构的对象数组,其中所有 parents
成为包含 children
数组的属性,每个数组都包含不同的对象并链接到图像或到正文?
{
theater: [
How_to_be_a_regisseur: {
img: "path",
text: "blah blah"
},
WOOW: {
img: "path",
text: "blah blah"
}
],
varia:[...{[][]}]
}
var list = [
{ parent: "theater", child: "How_to_be_a_regisseur", text: "Project 1!↵Phasellus a quam non arcu accumsan rhon…ium leo ante, vitae volutpat diam consectetur in." },
{ parent: "theater", child: "How_to_be_a_regisseur", img: "./content/theater/How_to_be_a_regisseur/img/download.jpeg" },
{ parent: "theater", child: "How_to_be_a_regisseur", img: "./content/theater/How_to_be_a_regisseur/img/rsz_namibia_will_burrard_lucas_wwf_us_1.jpg" },
{ parent: "theater", child: "WOOWW", text: "Project 1!↵Phasellus a quam non arcu accumsan rhon…ium leo ante, vitae volutpat diam consectetur in." },
{ parent: "varia", child: "gggggg", text: "Project 1!↵Phasellus a "},
{ parent: "varia", child: "gggggg", img: "./content/varia/gggggg/img/download.jpeg"},
{ parent: "varia", child: "gggggg", img: "./content/varia/gggggg/img/rsz_namibia_will_burrard_lucas_wwf_us_1.jpg"},
{ parent: "about", child: null, img: "./content/about/img/download.jpeg"}
];
var new_list = list.reduce((accumulator, currentValue, currentIndex, array) => {
var parent = currentValue.parent;
var child = currentValue.child;
if (!accumulator[parent]) {
accumulator[parent] = [];
}
if (!accumulator[parent][child]) {
accumulator[parent][child] = [];
}
accumulator[parent][child].push({
text: currentValue.text,
img: currentValue.img
});
return accumulator;
}, {});
console.log(new_list);
我以某种方式重组了它。
我正在构建一个基于小文件夹的 cms。我正在使用 php 提取文件夹的所有路径,我想使用 javascript 来构建网站。我知道我可以在 php 中做任何事情,但我在 javascript 中更流利,因此我想使用它而不是 php。无论如何 php 我得到这个列表:
0: {parent: "theater", child: "How_to_be_a_regisseur", text: "Project 1!↵Phasellus a quam non arcu accumsan rhon…ium leo ante, vitae volutpat diam consectetur in."}
1: {parent: "theater", child: "How_to_be_a_regisseur", img: "./content/theater/How_to_be_a_regisseur/img/download.jpeg"}
2: {parent: "theater", child: "How_to_be_a_regisseur", img: "./content/theater/How_to_be_a_regisseur/img/rsz_namibia_will_burrard_lucas_wwf_us_1.jpg"}
4: {parent: "theater", child: "WOOWW", text: "Project 1!↵Phasellus a quam non arcu accumsan rhon…ium leo ante, vitae volutpat diam consectetur in."}
...
12: {parent: "varia", child: "gggggg", text: "Project 1!↵Phasellus a
13: {parent: "varia", child: "gggggg", img: "./content/varia/gggggg/img/download.jpeg"}
14: {parent: "varia", child: "gggggg", img: "./content/varia/gggggg/img/rsz_namibia_will_burrard_lucas_wwf_us_1.jpg"}
...
20: {parent: "about", child: null, img: "./content/about/img/download.jpeg"}
parent
表示文件夹,child
表示子文件夹,img
或 text
表示该文件夹的内容。
有什么方法可以将此数组重组为具有此结构的对象数组,其中所有 parents
成为包含 children
数组的属性,每个数组都包含不同的对象并链接到图像或到正文?
{
theater: [
How_to_be_a_regisseur: {
img: "path",
text: "blah blah"
},
WOOW: {
img: "path",
text: "blah blah"
}
],
varia:[...{[][]}]
}
var list = [
{ parent: "theater", child: "How_to_be_a_regisseur", text: "Project 1!↵Phasellus a quam non arcu accumsan rhon…ium leo ante, vitae volutpat diam consectetur in." },
{ parent: "theater", child: "How_to_be_a_regisseur", img: "./content/theater/How_to_be_a_regisseur/img/download.jpeg" },
{ parent: "theater", child: "How_to_be_a_regisseur", img: "./content/theater/How_to_be_a_regisseur/img/rsz_namibia_will_burrard_lucas_wwf_us_1.jpg" },
{ parent: "theater", child: "WOOWW", text: "Project 1!↵Phasellus a quam non arcu accumsan rhon…ium leo ante, vitae volutpat diam consectetur in." },
{ parent: "varia", child: "gggggg", text: "Project 1!↵Phasellus a "},
{ parent: "varia", child: "gggggg", img: "./content/varia/gggggg/img/download.jpeg"},
{ parent: "varia", child: "gggggg", img: "./content/varia/gggggg/img/rsz_namibia_will_burrard_lucas_wwf_us_1.jpg"},
{ parent: "about", child: null, img: "./content/about/img/download.jpeg"}
];
var new_list = list.reduce((accumulator, currentValue, currentIndex, array) => {
var parent = currentValue.parent;
var child = currentValue.child;
if (!accumulator[parent]) {
accumulator[parent] = [];
}
if (!accumulator[parent][child]) {
accumulator[parent][child] = [];
}
accumulator[parent][child].push({
text: currentValue.text,
img: currentValue.img
});
return accumulator;
}, {});
console.log(new_list);
我以某种方式重组了它。