【问题标题】:Recursive function with an Object in JSJS中带有对象的递归函数
【发布时间】:2018-05-05 17:01:06
【问题描述】:

我有一个数组,其中包含可能具有第 n 级深度的对象。

类似这样的:

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'}  

        ]}  
    ]}

]

我只需要在不同的数组上推送 'Url' 属性和 children 属性(如果存在),但要保留深度。

所以新数组应该是这样的:

const newArray = [

    {url: '/pictures'},
    {url: '/user/:username', children:[
        {url: '/user/:username/highlights', children:[
                {url: '/user/:username/highlights'} 
        ]}  
    ]}

]

你能帮帮我吗?

谢谢

【问题讨论】:

标签: javascript arrays object recursion


【解决方案1】:

您可以将destructuring assignment 用于所需的键,并使用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; }

【讨论】:

    【解决方案2】:

    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);

    【讨论】:

    • 如果你已经在使用 ES6 语法,你也可以使用@Nina Scholz 解决方案
    【解决方案3】:

    这是一种简单易懂的方法以及 一个小提琴示例,您可以在其中进一步测试:

    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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多