How to search both parent and child object in Javascript/Lodash/ES6?

后端 未结 1 1643
隐瞒了意图╮
隐瞒了意图╮ 2021-01-24 08:36

So I\'m using Vue to filter a list. This consists of the following structure (Baum Hierarchy if anyone is interested):

 [
  {
    \"id\": 61,
    \"created_at\":         


        
相关标签:
1条回答
  • 2021-01-24 08:58

    you can do it with recursive function

    var res = _.reduce(this.sortable, function reducer(result, item) {
        if (item.name.search(new RegExp(search, "i")) !== -1) {
            result.items = _.concat(result.items, result.parent || item);
        }
        result.items = _.concat(
            result.items, 
           _.reduce(item.children, reducer, {parent: item, items: []}).items
        )
        return result;
    }, {items: []}).items;
    
    0 讨论(0)
提交回复
热议问题