javascript - How to remove hidden element from the array using recursive method -
i using tree structure show types not hidden. deleting type hidden = true
which working fine perfectly
var filtertypes = function (types) { (var = 0, l = types.length; < 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 not edit data (i.e. types), want create new array non-hidden values. want function provide types
data , returns me types not hidden.
note:- types structure 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 hidden should output follows
[1,2,3] 1.children should should [11, 12] #as 13 hidden 11.children should [111] 12.children should nil #as 121 hidden 3.children should nil #as 31, 32 hidden
you have create new arrays , objects in every place modified original before:
var filtertypes = function (types) { var newtypes = []; (var = 0, l = types.length; < l; i++) { var type = types[i]; if (type && !type.hidden) { var newtype = extend({}, type); //replace extend object.assign, angular.extend or similar if (type.children) { newtype.children = filtertypes(type.children); } newtypes.push(newtype); } } return newtypes; };
you immutable datastructures, immutable.js, might helpful.
Comments
Post a Comment