Javascript js每个循环为对象或数组添加一个深度
我有这个页面URL数组,现在我需要对它建立一个层次结构 因此:Javascript js每个循环为对象或数组添加一个深度,javascript,arrays,object,Javascript,Arrays,Object,我有这个页面URL数组,现在我需要对它建立一个层次结构 因此: allPages = [ { "url": "/polygon/color/red.html", "name": "Red" }, { "url": "/polygon/color/blue.html", "name": "Blue" }, { "url": "/polygon/shape/tri.html", "name": "Triangle" }, { "ur
allPages = [
{ "url": "/polygon/color/red.html", "name": "Red" },
{ "url": "/polygon/color/blue.html", "name": "Blue" },
{ "url": "/polygon/shape/tri.html", "name": "Triangle" },
{ "url": "/weight/heavy.html", "name": "Heavy Item" }
];
为此:
siteMap = [
polygon:
color:
[{url:"red.html", name:"Red"}],
[{url:"blue.html", name:"Blue"}],
shape:
[{url:"tri.html", name:"Triangle"}],
weight:
[{url:"heavy.html", name:"Heavy Item"}],
];
最后的结构可以是对象或数组。但我只能使用JS,不能使用jQuery或php
编辑:将输入数据更改为对象数组。很抱歉让这件事变得更难。提琴:
几个步骤:
首先,我们将字符串拆分为数组:
for(i in all){
all[i] = all[i].substring(1).split("/");
}
接下来,我们进行递归插入:
function insert(o, a){
if(a.length == 0){return; }
if(!o[a[0]]){
o[a[0]] = {};
}
insert(o[a[0]], a.slice(1));
}
我们这样开始递归:
ans = {};
all.forEach(function(entry){
insert(ans, entry);
});
var allPages = [
"/polygon/color/red.html",
"/polygon/color/green.html",
"/polygon/color/blue.html",
"/polygon/shape/tri.html",
"/polygon/shape/rect.html",
"/weight/heavy.html",
"/weight/light.html"
];
var siteMap = {};
for (var i in allPages) {
var fragments = allPages[i].match(/[^\/]+/g);
if (!!fragments) {
var currentMember = siteMap;
for (var j in fragments) {
fragment = fragments[j];
if(!currentMember.hasOwnProperty(fragment)) {
currentMember[fragment] = {};
}
currentMember = currentMember[fragment];
}
}
}
都做完了。结果树现在位于ans对象中:
console.log(ans);
更新:此代码使最后一级成为数组:小提琴:
几个步骤:
首先,我们将字符串拆分为数组:
for(i in all){
all[i] = all[i].substring(1).split("/");
}
接下来,我们进行递归插入:
function insert(o, a){
if(a.length == 0){return; }
if(!o[a[0]]){
o[a[0]] = {};
}
insert(o[a[0]], a.slice(1));
}
我们这样开始递归:
ans = {};
all.forEach(function(entry){
insert(ans, entry);
});
var allPages = [
"/polygon/color/red.html",
"/polygon/color/green.html",
"/polygon/color/blue.html",
"/polygon/shape/tri.html",
"/polygon/shape/rect.html",
"/weight/heavy.html",
"/weight/light.html"
];
var siteMap = {};
for (var i in allPages) {
var fragments = allPages[i].match(/[^\/]+/g);
if (!!fragments) {
var currentMember = siteMap;
for (var j in fragments) {
fragment = fragments[j];
if(!currentMember.hasOwnProperty(fragment)) {
currentMember[fragment] = {};
}
currentMember = currentMember[fragment];
}
}
}
都做完了。结果树现在位于ans对象中:
console.log(ans);
更新:此代码使最后一级成为数组:小提琴:
几个步骤:
首先,我们将字符串拆分为数组:
for(i in all){
all[i] = all[i].substring(1).split("/");
}
接下来,我们进行递归插入:
function insert(o, a){
if(a.length == 0){return; }
if(!o[a[0]]){
o[a[0]] = {};
}
insert(o[a[0]], a.slice(1));
}
我们这样开始递归:
ans = {};
all.forEach(function(entry){
insert(ans, entry);
});
var allPages = [
"/polygon/color/red.html",
"/polygon/color/green.html",
"/polygon/color/blue.html",
"/polygon/shape/tri.html",
"/polygon/shape/rect.html",
"/weight/heavy.html",
"/weight/light.html"
];
var siteMap = {};
for (var i in allPages) {
var fragments = allPages[i].match(/[^\/]+/g);
if (!!fragments) {
var currentMember = siteMap;
for (var j in fragments) {
fragment = fragments[j];
if(!currentMember.hasOwnProperty(fragment)) {
currentMember[fragment] = {};
}
currentMember = currentMember[fragment];
}
}
}
都做完了。结果树现在位于ans对象中:
console.log(ans);
更新:此代码使最后一级成为数组:小提琴:
几个步骤:
首先,我们将字符串拆分为数组:
for(i in all){
all[i] = all[i].substring(1).split("/");
}
接下来,我们进行递归插入:
function insert(o, a){
if(a.length == 0){return; }
if(!o[a[0]]){
o[a[0]] = {};
}
insert(o[a[0]], a.slice(1));
}
我们这样开始递归:
ans = {};
all.forEach(function(entry){
insert(ans, entry);
});
var allPages = [
"/polygon/color/red.html",
"/polygon/color/green.html",
"/polygon/color/blue.html",
"/polygon/shape/tri.html",
"/polygon/shape/rect.html",
"/weight/heavy.html",
"/weight/light.html"
];
var siteMap = {};
for (var i in allPages) {
var fragments = allPages[i].match(/[^\/]+/g);
if (!!fragments) {
var currentMember = siteMap;
for (var j in fragments) {
fragment = fragments[j];
if(!currentMember.hasOwnProperty(fragment)) {
currentMember[fragment] = {};
}
currentMember = currentMember[fragment];
}
}
}
都做完了。结果树现在位于ans对象中:
console.log(ans);
更新:此代码将最后一级设置为数组:您可以使用类似以下内容:
ans = {};
all.forEach(function(entry){
insert(ans, entry);
});
var allPages = [
"/polygon/color/red.html",
"/polygon/color/green.html",
"/polygon/color/blue.html",
"/polygon/shape/tri.html",
"/polygon/shape/rect.html",
"/weight/heavy.html",
"/weight/light.html"
];
var siteMap = {};
for (var i in allPages) {
var fragments = allPages[i].match(/[^\/]+/g);
if (!!fragments) {
var currentMember = siteMap;
for (var j in fragments) {
fragment = fragments[j];
if(!currentMember.hasOwnProperty(fragment)) {
currentMember[fragment] = {};
}
currentMember = currentMember[fragment];
}
}
}
可能会得到增强,特别是在末端叶子是对象的事实上,
但它是有效的。您可以使用以下内容:
ans = {};
all.forEach(function(entry){
insert(ans, entry);
});
var allPages = [
"/polygon/color/red.html",
"/polygon/color/green.html",
"/polygon/color/blue.html",
"/polygon/shape/tri.html",
"/polygon/shape/rect.html",
"/weight/heavy.html",
"/weight/light.html"
];
var siteMap = {};
for (var i in allPages) {
var fragments = allPages[i].match(/[^\/]+/g);
if (!!fragments) {
var currentMember = siteMap;
for (var j in fragments) {
fragment = fragments[j];
if(!currentMember.hasOwnProperty(fragment)) {
currentMember[fragment] = {};
}
currentMember = currentMember[fragment];
}
}
}
可能会得到增强,特别是在末端叶子是对象的事实上,
但它是有效的。您可以使用以下内容:
ans = {};
all.forEach(function(entry){
insert(ans, entry);
});
var allPages = [
"/polygon/color/red.html",
"/polygon/color/green.html",
"/polygon/color/blue.html",
"/polygon/shape/tri.html",
"/polygon/shape/rect.html",
"/weight/heavy.html",
"/weight/light.html"
];
var siteMap = {};
for (var i in allPages) {
var fragments = allPages[i].match(/[^\/]+/g);
if (!!fragments) {
var currentMember = siteMap;
for (var j in fragments) {
fragment = fragments[j];
if(!currentMember.hasOwnProperty(fragment)) {
currentMember[fragment] = {};
}
currentMember = currentMember[fragment];
}
}
}
可能会得到增强,特别是在末端叶子是对象的事实上,
但它是有效的。您可以使用以下内容:
ans = {};
all.forEach(function(entry){
insert(ans, entry);
});
var allPages = [
"/polygon/color/red.html",
"/polygon/color/green.html",
"/polygon/color/blue.html",
"/polygon/shape/tri.html",
"/polygon/shape/rect.html",
"/weight/heavy.html",
"/weight/light.html"
];
var siteMap = {};
for (var i in allPages) {
var fragments = allPages[i].match(/[^\/]+/g);
if (!!fragments) {
var currentMember = siteMap;
for (var j in fragments) {
fragment = fragments[j];
if(!currentMember.hasOwnProperty(fragment)) {
currentMember[fragment] = {};
}
currentMember = currentMember[fragment];
}
}
}
可能会得到增强,特别是在末端叶子是对象的事实上,
但是它是有效的。这听起来像是某种分配,你能开始尝试必要的代码吗。一个好的第一步是确定一个
/
@BillPull lol的方法,这不是一个作业,即使是,这对这个问题重要吗?它实际上比看起来更难。通常在堆栈溢出时,如果您尝试先解决它,您会得到更好的答案。这样,当你陷入困境时,人们可以帮助你,而不仅仅是为了一个非常具体的目的为你做工作question@BillPull我已经为此工作了两天了。现在尝试了20多种方法。我不可能跟踪所有的人。我不认为我最后一次尝试不工作有什么意义,因为我认为它甚至不接近正确的道路:(这听起来像是某种分配,你能开始尝试必要的代码吗?好的第一步是用某种方式识别/
@BillPull lol,这不是分配,即使是,这对这个问题有关系吗?实际上比看起来更难。通常在堆栈溢出时,你会得到更好的answ。)如果你试图先解决它,人们会在你陷入困境时帮助你,而不仅仅是为了一个非常具体的目的为你做工作question@BillPull我已经为此工作了两天。现在尝试了20多种方法。我不可能跟踪所有这些方法。我不认为把我最后一次不工作的尝试放在pted,因为我认为它甚至不接近正确的路径:(这听起来像是某种分配,你能开始尝试必要的代码吗?好的第一步是用某种方式识别/
@BillPull lol,这不是分配,即使是,这对这个问题有关系吗?实际上比看起来更难。通常在堆栈溢出时,你会得到更好的answ。)如果你试图先解决它,人们会在你陷入困境时帮助你,而不仅仅是为了一个非常具体的目的为你做工作question@BillPull我已经为此工作了两天。现在尝试了20多种方法。我不可能跟踪所有这些方法。我不认为把我最后一次不工作的尝试放在pted,因为我认为它甚至不接近正确的路径:(这听起来像是某种分配,你能开始尝试必要的代码吗?好的第一步是用某种方式识别/
@BillPull lol,这不是分配,即使是,这对这个问题有关系吗?实际上比看起来更难。通常在堆栈溢出时,你会得到更好的answ。)如果你试图先解决它,人们会在你陷入困境时帮助你,而不仅仅是为了一个非常具体的目的为你做工作question@BillPull我已经为此工作了两天。现在尝试了20多种方法。我不可能跟踪所有这些方法。我不认为把我最后一次不工作的尝试放在pted,因为我认为它甚至不接近正确的路径:(当数组中的项目是对象时,是否可以执行类似的过程?例如,allPages=[{“url”:“/polygon/color/red.html”,“name”:“/polygon/color/blue.html”,“name”:“/polygon/shape/tri.html”,“name”:“/Triangle”},{“url”:“/weight/heavy.html”,“name”:“heavy Item”}];当数组中的项是对象时,是否可以执行类似的过程?例如,allPages=[{“url”:“/polygon/color/red.html”,“name”:“red”},{“url”:“polygon/shape/tri.html”,“name”:“red”},{“polygon/color/blue.html”,“name”:“blue”},{”url:“/weight/heavy.html”,“name:“/heavy Item”}];当数组中的项是对象时,是否可以执行类似的过程?例如,allPages=[{“url”:“/polygon/color/red.html”,“name:“red”},{“url”:“/polygon/color/blue.html”,“name:“blue”},{“url”:/polygon/shape/tri