te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>algorithm - iterating through array of nested objects with javascriptlodash - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

algorithm - iterating through array of nested objects with javascriptlodash - Stack Overflow

programmeradmin4浏览0评论

I have an array of objects that represents a nested navigation list.

[
    {
        name: 'one',
        link: 'blah/blah',
        pages: [
            {
               name: 'one A'
               link: 'blah/blah',
               pages: []
            },
            {
               name: 'one B'
               link: 'blah/blah',
               pages: []
            },
            {
               name: 'one C'
               link: null,
               pages: [
                   {
                       name: 'one C I'
                       link: 'blah/blah',
                       pages: []
                   }
               ]
            }
        ]
    } 
]

The first level of objects can have a link and pages the nested objects will either have a link or pages. I can't assume a limit to the depth of the nesting. I need an object for each state that includes its name, its link if it exists and all of its parents. My current solution does not account for deeper than 3 levels of nesting and adding support for each layer is laborious.

I also need to be able to search the resulting array of objects to get their link later on if that makes a difference to the solution.

I need a javascript solution but can also (and would like to) use the functions contained in the lodash library

I have an array of objects that represents a nested navigation list.

[
    {
        name: 'one',
        link: 'blah/blah',
        pages: [
            {
               name: 'one A'
               link: 'blah/blah',
               pages: []
            },
            {
               name: 'one B'
               link: 'blah/blah',
               pages: []
            },
            {
               name: 'one C'
               link: null,
               pages: [
                   {
                       name: 'one C I'
                       link: 'blah/blah',
                       pages: []
                   }
               ]
            }
        ]
    } 
]

The first level of objects can have a link and pages the nested objects will either have a link or pages. I can't assume a limit to the depth of the nesting. I need an object for each state that includes its name, its link if it exists and all of its parents. My current solution does not account for deeper than 3 levels of nesting and adding support for each layer is laborious.

I also need to be able to search the resulting array of objects to get their link later on if that makes a difference to the solution.

I need a javascript solution but can also (and would like to) use the functions contained in the lodash library

Share Improve this question asked Jul 25, 2014 at 10:32 MarkMark 3,1976 gold badges40 silver badges78 bronze badges 5
  • 1 I think that the best would be to write a recursive function that takes each nested list as a parameter. Call the function again if the nested list is not empty. – Trace Commented Jul 25, 2014 at 10:45
  • thanks I figured that, I am not really sure how to keep track of all the parents while iterating though – Mark Commented Jul 25, 2014 at 10:47
  • Not sure what you mean. If you just loop through the top level, then call the same function on the sub levels, you will get through every 'pages' property that's in the list. – Trace Commented Jul 25, 2014 at 10:50
  • Yes but the resulting object needs to contain a list of all of its parents – Mark Commented Jul 25, 2014 at 10:56
  • for example the object that is created for 'one C I', needs a property that contains 'one C' and 'one' as they are its parents – Mark Commented Jul 25, 2014 at 10:58
Add a ment  | 

1 Answer 1

Reset to default 13

Here is a way to loop through the array recursively:

http://jsfiddle/yLnZe/37/

var arrPages = [{
        name: 'one',
        link: 'blah/blah',
        pages: [{
               name: 'one A', 
               link: 'blah/blah',
               pages: []
            },
            {
               name: 'one B', 
               link: 'blah/blah',
               pages: []
            },
            {
               name: 'one C', 
               link: null,
               pages: [{
                       name: 'one C I', 
                       link: 'blah blah',
                       pages: []
                   }]
            }]
    }]; 

function recursiveFunction(collection){ 
    _.each(collection, function(model){ 
        console.log(model); 
        if(model.pages.length > 0){ 
            recursiveFunction(model.pages); 
        }
    }); 
}; 

recursiveFunction(arrPages); 

Is this what you need, or do you need anything more specifically? Based on your last ments, I'm a bit confused if you need anything else.

发布评论

评论列表(0)

  1. 暂无评论