Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用递归方法从数组中删除隐藏元素_Javascript_Jquery_Arrays_Recursion - Fatal编程技术网

Javascript 如何使用递归方法从数组中删除隐藏元素

Javascript 如何使用递归方法从数组中删除隐藏元素,javascript,jquery,arrays,recursion,Javascript,Jquery,Arrays,Recursion,我使用树结构来显示未隐藏的类型。 为此,我删除了hidden=true工作正常的类型 var filtertypes = function (types) { for (var i = 0, l = types.length; i < l; i++) { var type = types[i]; if (type && !type.hidden) { if (type.text) { type.n = ty

我使用树结构来显示未隐藏的类型。 为此,我删除了
hidden=true
工作正常的类型

var filtertypes = function (types) {
    for (var i = 0, l = types.length; i < l; i++) {
      var type = types[i];
      if (type && !type.hidden) {
        if (type.text) {
          type.n = type.text
          type.id = type.id;
        }
        if (type.children) {
          filtertypes(type.children);
        }
      } else {
         types.splice(i, 1);
         filtertypes(types);
      }
    }
    return types;
  };
假设13、121、21、31、32是隐藏的,我应该得到如下输出

[1,2,3]

Where 1.children should be should get [11, 12] #as 13 is hidden
        11.children should be [111]
        12.children should be nil #as 121 is hidden
        3.children should be nil #as 31, 32 are hidden

就在我脑子里。可能需要进行一些测试和调整。

使用
Array.prototype.filter()
函数

(如果不应修改原始对象,请不要忘记创建其副本)


请参见

您必须在之前修改原始阵列的每个位置创建新阵列和对象:

var filtertypes = function (types) {
    var newTypes = [];
    for (var i = 0, l = types.length; i < l; i++) {
      var type = types[i];
      if (type && !type.hidden) {
        var newType = extend({}, type); //replace extend with Object.assign, angular.extend or similar
        if (type.children) {
          newType.children = filtertypes(type.children);
        }
        newTypes.push(newType);
      }
    }
    return newTypes;
  };
var filtertypes=函数(类型){
var newTypes=[];
对于(变量i=0,l=types.length;i

您还可以研究不可变的数据结构,这可能会有所帮助。

可以使用
Array.prototype.filter()
…返回新的数组,而保留原始数组。在jsfiddle中提供一些示例数据我是否正确地理解了这一点,即您想要一个与
filtertypes
功能相同但不更改其输入的函数?我有一个树结构,我将所有树结构作为输入提供,现在我想要所有的树结构,但一个不是隐藏的。我认为这不会起作用,因为您仍然在变异类型对象,所以它只适用于顶级数组。看起来它起作用了:)+1.我可以写一个问题newType=type;newType.n=type.text;我的意思是,我将首先复制对象,然后仅修改某些值。否,因为这并不构成副本,
newType
将只是对同一对象的引用。但是您可以使用
var newType=Object.assign({},type)用于克隆对象。感谢您的快速回复
var newType=Object.assign({},type)对我不起作用可能是因为我使用的是angularjs,所以我使用的是
var newType=angular.copy(type)
它会起作用吗?就像我说的,你需要as Object.assign不是所有浏览器都支持的。您不需要
angular.copy
,它可以进行深度复制(它可以工作,但需要更多的资源)。改用这个:
var newType=angular.extend({},type)。它与
对象相同。分配
function filterInputs(types){
        types = types.filter(function(type){
           if(type.hidden){
              return false;
           }
           if(type.children){
              filterInputs(type.children);
           }
           if(type.text){
              type.n = type.text
              type.id = type.id;
           }
           return true;
       });
    }
    var types = [
    {nr:1, hidden:false, children:[
        {nr:11, hidden:false, children:[{nr:111, hidden:false}]},
        {nr:12, hidden:false, children:[{nr:121, hidden:true}]},
        {nr:13, hidden:true}
    ]},
    {nr:2, hidden:false, children:[
        {nr:21, hidden:true, children:[{nr:211, hidden:false}, {nr:212, hidden:false}]},
        {nr:22, hidden:false, children:[{nr:221, hidden:false}, {nr:222, hidden:false}]}
    ]},
    {nr:3, hidden:false, children:[
        {nr:31, hidden:true, children:[{nr:311, hidden:false}, {nr:312, hidden:false}]},
        {nr:32, hidden:true, children:[{nr:321, hidden:false}, {nr:322, hidden:false}]}
    ]}
];

// To ensure the original object tree isn't modified, deepclone it first
var copyOfTypes = JSON.parse(JSON.stringify(types))

// Filter the types array
var nonHiddenTypes = copyOfTypes.filter(filterTypes);

function filterTypes(type){
    if(type.hidden) {
        return false; // current item will NOT be in the new array
    }

    if(type.children) {
        type.children = type.children.filter(filterTypes); // recursive filter the children
    }

    return true; // current item will be in the new array
}
var filtertypes = function (types) {
    var newTypes = [];
    for (var i = 0, l = types.length; i < l; i++) {
      var type = types[i];
      if (type && !type.hidden) {
        var newType = extend({}, type); //replace extend with Object.assign, angular.extend or similar
        if (type.children) {
          newType.children = filtertypes(type.children);
        }
        newTypes.push(newType);
      }
    }
    return newTypes;
  };