javascript - How to build a menu list object recursively in JavaScript?
问题描述
With an array of
['/social/swipes/women', '/social/swipes/men', '/upgrade/premium'];
I'd like to construct an map object that looks like:
{
'social': {
swipes: {
women: null,
men: null
}
},
'upgrade': {
premium: null
}
}
const menu = ['/social/swipes/women', '/social/likes/men', '/upgrade/premium'];
const map = {};
const addLabelToMap = (root, label) => {
if(!map[root]) map[root] = {};
if(!map[root][label]) map[root][label] = {};
}
const buildMenuMap = menu => {
menu
// make a copy of menu
// .slice returns a copy of the original array
.slice()
// convert the string to an array by splitting the /'s
// remove the first one as it's empty
// .map returns a new array
.map(item => item.split('/').splice(1))
// iterate through each array and its elements
.forEach((element) => {
let root = map[element[0]] || "";
for (let i = 1; i < element.length; i++) {
const label = element[i];
addLabelToMap(root, label)
// set root to [root][label]
//root = ?
root = root[label];
}
});
}
buildMenuMap(menu);
console.log(map);
But I'm unsure how to switch the value of root
.
What do I set root
to so that it recursively calls addLabelToMap
with
'[social]'
, 'swipes' => '[social][swipes]'
, 'women' => '[social][swipes]'
, 'men'
?
I've used root = root[element]
but it's giving an error.
Alternative solutions would be great, but I'd like to understand why this isn't working fundamentally.
解决方案
这个问题是关于创建对象并维护它的状态,同时循环遍历input
数组和基于/
.
这可以使用Array.reduce
我们从空对象开始的地方来完成,在循环input
中我们开始填充它,并且对于每个字符串中的最后一个单词,我们将值分配null
给对象属性。
let input = ['/social/swipes/women', '/social/swipes/men', '/upgrade/premium'];
let output = input.reduce((o, d) => {
let keys = d.split('/').filter(d => d)
keys.reduce((k, v, i) => {
k[v] = (i != keys.length - 1)
? k[v] || {}
: null
return k[v]
}, o)
return o
}, {})
console.log(output)
推荐阅读
- r - 转换 R 表避免重复
- python-2.7 - 在 Ubuntu 上安装与 GPU (Cuda 9.0) 兼容的预构建 Caffe
- angular - 是否可以将@ContentChild 与基类一起使用?
- javascript - 为 Firefox 扩展设置弹出窗口标题
- python - Python在编译正则表达式时返回不平衡括号错误
- jquery - 从 Angular 4 升级到 6 后应用程序无法访问 jquery 插件
- python - 为什么我使用 Esearch 和 Efetch 时会收到 BioPython HTTPError: HTTP Error 400: Bad Request
- jquery - 如何使用 .each jQuery 将我的元素的 ID 设置为另一个元素的属性
- java - 锁定的灵活替代方案(选择性锁定)
- python - 使用与时间序列相关的 python 进行机器学习