删除多维数组树的属性-使用lodash或下划线或js

时间:2018-10-30 15:48:56

标签: javascript typescript lodash

示例树:

[
    { id: 32, name: 'unique name', children: [] },
    { id: 43, name: 'unique name', children: [] },
    { id: 55, name: 'unique name', children: [
        { id: 31, name: 'unique name', children: [] },
        { id: 63, name: 'unique name', children: [] },
        { id: 84, name: 'unique name', children: [
            { id: 47, name: 'unique name', children: [] },
            { id: 56, name: 'unique name', children: [] }
        ] }
    ] }
    { id: 67, name: 'unique name', children: [
        { id: 78, name: 'unique name', children: [] },
        { id: 89, name: 'unique name', children: [] },
    ] },
    { id: 30, name: 'unique name', children: [] }
]

有人,请让我知道如何使用lodash或纯js删除树的名称属性?

输出应为:

{ id: 32,  children: [] },
{ id: 43,  children: [] },
{ id: 55,  children: [
{ id: 31,  children: [] },
{ id: 63,  children: [] },
{ id: 84,  children: [
{ id: 47,  children: [] },
{ id: 56,  children: [] }] }
] }
{ id: 67,  children: [
{ id: 78,  children: [] },
{ id: 89,  children: [] },
] },
{ id: 30,  children: [] }

2 个答案:

答案 0 :(得分:1)

只需选择所需的属性。您可以使用map函数:

let originArray = [
    { id: 32, name: 'unique name', children: [] },
    { id: 43, name: 'unique name', children: [] },
    {
        id: 55, name: 'unique name', children: [
            { id: 31, name: 'unique name', children: [] },
            { id: 63, name: 'unique name', children: [] },
            {
                id: 84, name: 'unique name', children: [
                    { id: 47, name: 'unique name', children: [] },
                    { id: 56, name: 'unique name', children: [] }
                ]
            }
        ]
    },
    {
        id: 67, name: 'unique name', children: [
            { id: 78, name: 'unique name', children: [] },
            { id: 89, name: 'unique name', children: [] },
        ]
    },
    { id: 30, name: 'unique name', children: [] }
]

let flattenArray = originArray.map(item => {
    return {
        id: item.id,
        children: item.children
    };
});

console.log(flattenArray);

答案 1 :(得分:0)

您可以通过简单的递归和forEach来做到这一点:

function removeName(node) {
  delete node['name'];
  node.children.forEach(removeName);
}

这个想法是从删除顶层的name开始,然后递归地对children数组中的每个节点再次进行此操作:

var tree = [{
    id: 32,
    name: 'unique name',
    children: []
  },
  {
    id: 43,
    name: 'unique name',
    children: []
  },
  {
    id: 55,
    name: 'unique name',
    children: [{
        id: 31,
        name: 'unique name',
        children: []
      },
      {
        id: 63,
        name: 'unique name',
        children: []
      },
      {
        id: 84,
        name: 'unique name',
        children: [{
            id: 47,
            name: 'unique name',
            children: []
          },
          {
            id: 56,
            name: 'unique name',
            children: []
          }
        ]
      }
    ]
  },
  {
    id: 67,
    name: 'unique name',
    children: [{
        id: 78,
        name: 'unique name',
        children: []
      },
      {
        id: 89,
        name: 'unique name',
        children: []
      },
    ]
  },
  {
    id: 30,
    name: 'unique name',
    children: []
  }
];


function removeName(node) {
  delete node['name'];
  node.children.forEach(removeName);
}

tree.forEach(removeName);
console.log(tree);