【问题标题】:recursive search and push in complex array/object递归搜索并推入复杂的数组/对象
【发布时间】:2018-07-30 11:52:11
【问题描述】:

我已将此 plunker https://plnkr.co/edit/CIGAA5BmiKU4hCMsOaIB?p=preview 作为参考,但现在我需要动态数组操作

[
{
    title: 'Menu 1',
    id :1,
    hide : true,
    children: [],
},
{
    title: 'Menu 2',
    hide : true,
    id :2,
    children: [{
        title: 'Sub Menu 2',
        hide : true,
        id :3,
        children: [{
            title: 'Sub Sub Menu 2',
            hide : true,
            id :4,
            children: [{
                title: 'Sub Sub Menu 2, Sibling 1',
                hide : true,
                id :6,
                children: []
            },
            {
                title: 'Sub Sub Sub Menu 2, Sibling 2',
                hide : true,
                id :12,
                children: []
            }]
        }]
    }]

},
{
    title: 'Menu 3',
    hide : true,
    id :14,
    children: []
}
]; 

现在我必须将孩子推送到 id 为 6 的对象中,并且每次操作后都需要更新整个对象。

我正在使用角度 5

我用过下面这个方法

find(id, items,newData) {
  var i = 0, found;
  for (; i < items.length; i++) {
    if (items[i].id === id) {
      items[i].children=newData;
      return items;
} else if (_.isArray(items[i].children)) {
  found = this.find(id, items[i].children,newData);
  if (found) {
    return false;

      }
    }
  }
}

这里基本上 newData 是我需要推送的数组, items 是我推送后应该更新的主要对象

如果我在某个地方错了,请纠正我。如果 id 为 3 的元素有 id 为 4 的子元素,那么现在是否不应将其推送到同一个父 id 中。

所有对象都具有相同的结构,例如 newData 也有子对象

【问题讨论】:

  • 我不太明白你代码中的逻辑

标签: javascript angular typescript lodash


【解决方案1】:

创建和使用递归函数

var arr = [{
  title: 'Menu 1',
  id: 1,
  hide: true,
  children: [],
}, {
  title: 'Menu 2',
  hide: true,
  id: 2,
  children: [{
    title: 'Sub Menu 2',
    hide: true,
    id: 3,
    children: [{
      title: 'Sub Sub Menu 2',
      hide: true,
      id: 4,
      children: [{
        title: 'Sub Sub Menu 2, Sibling 1',
        hide: true,
        id: 6,
        children: [{
          title: 'Sub Sub Menu 2, Sibling 1',
          hide: true,
          id: 7,
          children: []
        }]
      }, {
        title: 'Sub Sub Sub Menu 2, Sibling 2',
        hide: true,
        id: 12,
        children: []
      }]
    }]
  }]
}, {
  title: 'Menu 3',
  hide: true,
  id: 14,
  children: []
}];

// a recursive function which accepts an array and the id , this id will be use to target the children of the object whose id matches
// looping through the array & checking if id matches
function findUpdate(array, id) {
  array.forEach(function(elem) {
    if (elem.id === id) {
      // push value to children of that object
      elem.children.push("pushed")
    } else {
       //check if children is an array and if it is empty
      if (Array.isArray(elem.children) && elem.children.length > 0) {
        //call the same function with the new arra
        findUpdate(elem.children, id)
      }
    }
  });

  console.log(arr)
}

console.log(findUpdate(arr, 12))

【讨论】:

    【解决方案2】:

    您返回的是 false 而不是 item。您想要替换或推送。

    var id = 6;
    var items = [{
            title: 'Menu 1',
            id: 1,
            hide: true,
            children: [],
        },
        {
            title: 'Menu 2',
            hide: true,
            id: 2,
            children: [{
                title: 'Sub Menu 2',
                hide: true,
                id: 3,
                children: [{
                    title: 'Sub Sub Menu 2',
                    hide: true,
                    id: 4,
                    children: [{
                            title: 'Sub Sub Menu 2, Sibling 1',
                            hide: true,
                            id: 6,
                            children: [{
                                title: 'Sub Sub Menu 2, Sibling 1',
                                hide: true,
                                id: 7,
                                children: []
                            }]
                        },
                        {
                            title: 'Sub Sub Sub Menu 2, Sibling 2',
                            hide: true,
                            id: 12,
                            children: []
                        }
                    ]
                }]
            }]
    
        },
        {
            title: 'Menu 3',
            hide: true,
            id: 14,
            children: []
        }
    ];
    var newData = [{
        title: 'new Data'
    }]
    var find = (id, items, newData) => {
        var i = 0,
            found;
        for (; i < items.length; i++) {
            if (items[i].id === id) {
                items[i].children.push(newData); //change here if you want add in existing array
                return items;
            } else if (items[i].children.length != 0) {
                found = this.find(id, items[i].children, newData);
                if (found) {
                    return items;
                }
            }
        }
        return items;
    }
    
    console.log(find(id, items, newData))

    【讨论】:

    • 我想将数组推送到特定 id 的孩子中,最后我需要最终更新整个数组。
    • 不检查您更新的响应不在 children 数组中。需要推送的新对象也具有与其他元素相同的结构。
    猜你喜欢
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2018-08-08
    • 2021-06-27
    相关资源
    最近更新 更多