2016-10-03 75 views
0

我有一个嵌套对象数组。这些对象采取以下两种形式之一:通过索引从嵌套数组中删除项目(递归)

// type a 
{ 
    value: 'some value' 
} 

// type b 
{ 
    array: [ 
     object of type a or b, 
     object of type a or b, 
     ... 
    ] 
} 

所以,基本阵列可以无限嵌套。鉴于一系列指数(我一直称它为“树”),我怎样才能在任意深度删除单个项目?

什么我到目前为止的例子:

const baseArray = [ 
    { value: 'some value' }, 
    { array: [ 
      { value: 'some value' }, 
      { array: [ 
        { value: 'some value' }, 
        { value: 'some value' }, 
       ], 
      }, 
      { value: 'some value' }, 
      { array: [ 
        { value: 'some value' }, 
        { array: [ 
          { value: 'delete me' }, 
          { value: 'some value' }, 
         ] 
        }, 
       ], 
      }, 
     ], 
    } 
] 

const tree = [1, 3, 1, 0] 

function deleteNested(tree, inputArray) { 
    const index = tree.shift(); 
    console.log(inputArray, index); 
    const child = inputArray[index].array; 
    if (tree.length > 0) { 
     console.log(child) 
     return deleteNested(tree, child); 
    } 
    return [ 
     ...inputArray.slice(0, index), 
     ...inputArray.slice(index + 1) 
    ] 
} 
const originalArray = baseArray.slice(0); 
console.log(deleteNested(tree, baseArray), originalArray); 

我要删除标记的对象给予它的“树”的位置:[1, 3, 1, 0]

  • 第一,看在1(指数1,而不是0)的初始阵列的值,
  • 则3值,
  • 再看看1个值,
  • 然后最后删除0值。

我上面有什么不起作用,但让我开始了。

函数需要递归才能在任何深度工作。理想情况下,不应该使用splice()来避免修改传入它的数组,而应该返回一个新数组。

+0

我猜递归一个,而不是一段时间,因为你已经知道提前迭代次数循环会更合适。 – Redu

回答

1

正如我在评论中所说,如果您事先知道迭代次数,则不应使用递归方法。 while循环是理想的,例如;

function delNestedItem(a,dm){ 
 
    var i = 0; 
 
    while (i < dm.length-1) a = a[dm[i++]].array; 
 
    a.splice(dm[i],1); 
 
} 
 

 
var data = [ 
 
    { value: 'some value' }, 
 
    { array: [ 
 
      { value: 'some value' }, 
 
      { array: [ 
 
        { value: 'some value' }, 
 
        { value: 'some value' }, 
 
       ], 
 
      }, 
 
      { value: 'some value' }, 
 
      { array: [ 
 
        { value: 'some value' }, 
 
        { array: [ 
 
          { value: 'delete me' }, 
 
          { value: 'some value' }, 
 
         ] 
 
        }, 
 
       ], 
 
      }, 
 
     ], 
 
    } 
 
      ], 
 

 
delMark = [1, 3, 1, 0]; 
 
delNestedItem(data,delMark); 
 
console.log(JSON.stringify(data,null,2));

0

这里是你如何能做到在1减少:

const baseArray = [{ 
 
    value: 'some value' 
 
}, { 
 
    array: [{ 
 
    value: 'some value' 
 
    }, { 
 
    array: [{ 
 
     value: 'some value' 
 
    }, { 
 
     value: 'some value' 
 
    }, ], 
 
    }, { 
 
    value: 'some value' 
 
    }, { 
 
    array: [{ 
 
     value: 'some value' 
 
    }, { 
 
     array: [{ 
 
     value: 'delete me' 
 
     }, { 
 
     value: 'some value' 
 
     }, ] 
 
    }, ], 
 
    }, ], 
 
}]; 
 

 
const tree = [1, 3, 1, 0]; 
 

 

 
var deleted = tree.reduce(function(pos, pathIndex, index, arr) { 
 
    if (index + 1 < arr.length) { 
 
    return pos.array 
 
     ? pos.array[pathIndex] 
 
     : pos[pathIndex]; 
 
    } else { 
 
    pos.array = pos.array 
 
      .slice(0, pathIndex) 
 
      .concat(pos.array.slice(pathIndex + 1)); 
 
    return pos; 
 
    } 
 

 
}, baseArray); 
 
      
 
      console.log(baseArray); 
 
     

0

您可以生成一个新的数组出给定数组的只与未删除的部分。

function ff(array, tree) { 
 
    function iter(array, level) { 
 
     var r = []; 
 
     array.forEach(function (a, i) { 
 
      if (tree[level] !== i) { 
 
       return r.push(a); 
 
      } 
 
      if (level + 1 !== tree.length && a.array) { 
 
       r.push({ array: iter(a.array, level + 1) }); 
 
      } 
 
     }); 
 
     return r; 
 
    } 
 
    return iter(array, 0); 
 
} 
 

 
var baseArray = [{ value: 'some value' }, { array: [{ value: 'some value' }, { array: [{ value: 'some value' }, { value: 'some value' }, ], }, { value: 'some value' }, { array: [{ value: 'some value' }, { array: [{ value: 'delete me' }, { value: 'some value' }, ] }, ], }, ], }], 
 
    tree = [1, 3, 1, 0], 
 
    copy = ff(baseArray, tree); 
 

 
console.log(copy); 
 
console.log(baseArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }