从动态创建的ObjectArray中删除一个对象

时间:2017-03-25 04:34:52

标签: javascript angularjs arrays multidimensional-array

我有一个对象数组 列表格式如下

var myFormData = [
{
    id: 1,
    name: "first name",
    type: "test",
    root: "/myfolder"
},
{
    id: 3,
    name: "your name",
    type: "test 2",
    root: "/myfolder2"
}, {
    id: 4,
    name: "your test",
    type: "test 3",
    root: "/myfold",
    child: [
        {
            id: 5,
            name: "name",
            type: "testf",
            root: "/myfoldertr"
        },
        {
            id: 6,
            name: "first-name",
            type: "test",
            root: "/myfolderoot",
            child: [
                {
                    id: 8,
                    name: "sub first name",
                    type: "test5",
                    root: "/myfoldertest"
                }, {
                    id: 9,
                    name: "first name root",
                    type: "test9",
                    root: "/myfolder",
                    child: [
                        {
                            id: 10,
                            name: "normal first name",
                            type: "test5",
                            root: "/myfoldertest"
                        }, {
                            id: 11,
                            name: "last first name",
                            type: "test5",
                            root: "/myfoldertest"
                        }
                    ]
                },
                {
                    id: 12,
                    name: "name Name",
                    type: "testf",
                    root: "/myfoldertr"
                }
            ]
        },
        {
            id: 7,
            name: "first name",
            type: "test",
            root: "/myfolder"
        }
    ]
}]

这种格式是用数据库创建的,所以我不能确定数据是准确的。有些时候他们有孩子或没有孩子。 我想删除一个对象,如果id等于给定的id(从编程中获取)例如:我想删除id = 11.

1 个答案:

答案 0 :(得分:3)

关键是你必须在目标数组中看。此代码段示例使用递归调用深入嵌套数组。

function deleteObj(target, id) {
  if (!Array.isArray(target)) return;
  target.forEach(function(item, index) {
    if (item.child) {
      target = deleteObj(item.child, id);
    }
    if (item.id === 11) {
      target.splice(index, 1);
    }
  });
}



var myFormData = [{
    id: 1,
    name: "first name",
    type: "test",
    root: "/myfolder"
  },
  {
    id: 3,
    name: "your name",
    type: "test 2",
    root: "/myfolder2"
  }, {
    id: 4,
    name: "your test",
    type: "test 3",
    root: "/myfold",
    child: [{
        id: 5,
        name: "name",
        type: "testf",
        root: "/myfoldertr"
      },
      {
        id: 6,
        name: "first-name",
        type: "test",
        root: "/myfolderoot",
        child: [{
            id: 8,
            name: "sub first name",
            type: "test5",
            root: "/myfoldertest"
          }, {
            id: 9,
            name: "first name root",
            type: "test9",
            root: "/myfolder",
            child: [{
              id: 10,
              name: "normal first name",
              type: "test5",
              root: "/myfoldertest"
            }, {
              id: 11,
              name: "last first name",
              type: "test5",
              root: "/myfoldertest"
            }]
          },
          {
            id: 12,
            name: "name Name",
            type: "testf",
            root: "/myfoldertr"
          }
        ]
      },
      {
        id: 7,
        name: "first name",
        type: "test",
        root: "/myfolder"
      }
    ]
  }
];

function deleteObj(target, id) {
  if (!Array.isArray(target)) return;
  target.forEach(function(item, index) {
    if (item.child) {
      target = deleteObj(item.child, id);
    }
    if (item.id === 11) {
      target.splice(index, 1);
    }
  });
}

deleteObj(myFormData, 11);
console.log(myFormData);