[英]How to remove hidden element from the array using recursive method
I am using Tree structure to show the types which are not hidden. 我正在使用Tree结构来显示未隐藏的类型。 For this i am deleting the type for which hidden = true
which working fine perfectly 为此,我删除了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;
};
But as this is not good to edit the data (ie types), so now i want to create a new array with only non-hidden values. 但是由于这不利于编辑数据(即类型),所以现在我想创建一个仅包含非隐藏值的新数组。 So i want a function to which i provide a types
as a data and it returns me all the types which are not hidden. 所以我想要一个函数,我向该函数提供types
作为数据,它返回所有未隐藏的类型。
Note:- types structure is as follow 注意:-类型结构如下
1 11 111
12 121
13
2 21 211
212
22 221
222
3 31 311
312
32 321
322
Suppose 13, 121, 21, 31, 32 are hidden i should get output as follows 假设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
You have to create new Arrays and Objects in every place where you modified the original before: 您必须在之前修改原始位置的每个位置创建新的数组和对象:
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;
};
You could also look into immutable datastructures, immutable.js , which might be helpful. 您还可以研究不可变的数据结构immutable.js ,这可能会有所帮助。
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;
});
}
Just from my head. 只是从我的头。 Probably needs to be tested and tweak a little. 可能需要测试并稍作调整。
Use the Array.prototype.filter()
function mdn 使用Array.prototype.filter()
函数mdn
(Don't forget to create a copy of the original object if it should not be modified) (如果不应该修改原始对象,请不要忘记为其创建副本)
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.