从 Javascript 中的复杂嵌套对象值构建字符串
Build string from complex nested object values in Javascript
我有以下输入。
[{ name: 'modules',
children: [ { name: 'home', children: [], allowed: true } ],
allowed: true },
{ name: 'modules',
children:
[ { name: 'new', children: [], allowed: true },
{ name: 'all', children: [], allowed: true } ],
allowed: true },
{ name: 'groups',
children:
[ { name: 'new', children: [], allowed: true },
{ name: 'all', children: [], allowed: true } ],
allowed: true },
{ name: 'users',
children:
[ { name: 'new', children: [], allowed: true },
{ name: 'all', children: [], allowed: true } ],
allowed: true },
{ name: 'settings',
children:
[ { name: 'generally', children: [], allowed: true },
{ name: 'personal', children: [], allowed: true } ],
allowed: true }]
我想从这样的值构建字符串:name.children.name.children 等等。我需要所有可能的组合。嵌套可以是无止境的。所以像 'test.test1.test2.test3' 这样的字符串是可能的。
所以这个例子的最终结果应该是:
'modules.home'
'modules.new'
'modules.all'
'groups.new'
'groups.all'
'users.new'
'users.all'
'settings.generally'
'settings.personal'
我目前的解决方案并没有真正奏效..
哪个是实现此目标的最佳和最有效的解决方案?提前致谢!
你可以使用递归函数,像这样
function recursiveNamer(currentList, currentNameString, result) {
var name = currentNameString === "" ? "" : currentNameString + ".";
currentList.forEach(function(currentObject) {
if (currentObject.children.length === 0) {
result.push(name + currentObject.name);
} else {
recursiveNamer(currentObject.children, name + currentObject.name, result);
}
});
return result;
}
console.log(recursiveNamer(inputData, "", []));
结果会是
[ 'modules.home',
'modules.new',
'modules.all',
'groups.new',
'groups.all',
'users.new',
'users.all',
'settings.generally',
'settings.personal' ]
您可以使用循环:
for(var i=0;i<data.length;i++){
for(var e=0;e<data[i].children.length;e++){
console.log(data[i].name+'.'+data[i].children[e].name);
}
}
我有以下输入。
[{ name: 'modules',
children: [ { name: 'home', children: [], allowed: true } ],
allowed: true },
{ name: 'modules',
children:
[ { name: 'new', children: [], allowed: true },
{ name: 'all', children: [], allowed: true } ],
allowed: true },
{ name: 'groups',
children:
[ { name: 'new', children: [], allowed: true },
{ name: 'all', children: [], allowed: true } ],
allowed: true },
{ name: 'users',
children:
[ { name: 'new', children: [], allowed: true },
{ name: 'all', children: [], allowed: true } ],
allowed: true },
{ name: 'settings',
children:
[ { name: 'generally', children: [], allowed: true },
{ name: 'personal', children: [], allowed: true } ],
allowed: true }]
我想从这样的值构建字符串:name.children.name.children 等等。我需要所有可能的组合。嵌套可以是无止境的。所以像 'test.test1.test2.test3' 这样的字符串是可能的。
所以这个例子的最终结果应该是:
'modules.home'
'modules.new'
'modules.all'
'groups.new'
'groups.all'
'users.new'
'users.all'
'settings.generally'
'settings.personal'
我目前的解决方案并没有真正奏效.. 哪个是实现此目标的最佳和最有效的解决方案?提前致谢!
你可以使用递归函数,像这样
function recursiveNamer(currentList, currentNameString, result) {
var name = currentNameString === "" ? "" : currentNameString + ".";
currentList.forEach(function(currentObject) {
if (currentObject.children.length === 0) {
result.push(name + currentObject.name);
} else {
recursiveNamer(currentObject.children, name + currentObject.name, result);
}
});
return result;
}
console.log(recursiveNamer(inputData, "", []));
结果会是
[ 'modules.home',
'modules.new',
'modules.all',
'groups.new',
'groups.all',
'users.new',
'users.all',
'settings.generally',
'settings.personal' ]
您可以使用循环:
for(var i=0;i<data.length;i++){
for(var e=0;e<data[i].children.length;e++){
console.log(data[i].name+'.'+data[i].children[e].name);
}
}