[英]Recursive function with an Object in JS
I have an array that contains objects that might have nth levels of depth. 我有一个数组,其中包含可能具有n级深度的对象。
Something like this: 像这样的东西:
const settings = [
{path: '/templates/pictures.php', url: '/pictures', label: 'Pictures', component: 'tab', template: 'default'},
{path: '/templates/post-article.php', url: '/user/:username', component: 'table', template: 'default', children:[
{path: '/templates/post-article-highlights.php', url: '/user/:username/highlights', component: 'table', template: 'default', children:[
{path: '/templates/post-article-highlights.php', url: '/user/:username/highlights', component: 'table', template: 'default'}
]}
]}
]
I need to push on a different array only the 'Url' property and the children property if present, preserving the depth though. 我需要在不同的数组上推送'Url'属性和children属性(如果存在),但保留深度。
So the new array should look like this: 所以新数组应如下所示:
const newArray = [
{url: '/pictures'},
{url: '/user/:username', children:[
{url: '/user/:username/highlights', children:[
{url: '/user/:username/highlights'}
]}
]}
]
Can you help me? 你能帮助我吗?
Thanks 谢谢
You could use a destructuring assignment for the wanted keys and use Array#map
for getting a new array with only the one property and use Object.assign
for the children objects by checking the children and if exist, take the urls from the children with a recursive call of the function. 您可以对所需的键使用解构分配 ,并使用
Array#map
获取仅具有一个属性的新数组,并通过检查子对象使用Object.assign
作为子对象,如果存在,则使用带有递归调用函数。
function getUrls(array) { return array.map(({ url, children }) => Object.assign({ url }, children && { children: getUrls(children) })); } var settings = [{ path: '/templates/pictures.php', url: '/pictures', label: 'Pictures', component: 'tab', template: 'default' }, { path: '/templates/post-article.php', url: '/user/:username', component: 'table', template: 'default', children: [{ path: '/templates/post-article-highlights.php', url: '/user/:username/highlights', component: 'table', template: 'default', children: [{ path: '/templates/post-article-highlights.php', url: '/user/:username/highlights', component: 'table', template: 'default' }] }] }], urls = getUrls(settings); console.log(urls);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const settings = [ {path: '/templates/pictures.php', url: '/pictures', label: 'Pictures', component: 'tab', template: 'default'}, {path: '/templates/post-article.php', url: '/user/:username', component: 'table', template: 'default', children:[ {path: '/templates/post-article-highlights.php', url: '/user/:username/highlights', component: 'table', template: 'default', children:[ {path: '/templates/post-article-highlights.php', url: '/user/:username/highlights', component: 'table', template: 'default'} ]} ]} ]; function childrenUrls(childrens){ return childrens.reduce(function(arr, obj){ var newObj = {url: obj.url}; if(obj.children) newObj.children = childrenUrls(obj.children); return arr.push(newObj), arr; }, []); } const newArray = childrenUrls(settings); console.log(newArray);
Here is an easy and understandable approach along with aa fiddle example where you can test it further: 这是一个简单易懂的方法,还有一个小提琴示例,您可以进一步测试它:
https://jsfiddle.net/eugensunic/pftkosb9/ https://jsfiddle.net/eugensunic/pftkosb9/
function findArray(array) {
var new_array = [];
for (var i = 0; i < array.length; i++) {
if (array[i].url) {
let obj = {};
obj['url'] = array[i].url;
obj['children'] = array[i].children;
new_array.push(obj);
}
if (Array.isArray(array[i].children)) {
new_array = new_array.concat(findArray(array[i].children));
}
}
return new_array;
}
console.log(findArray(settings));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.