recursion - Find node in javascript tree by some params -
i try write mixin underscore, can find node params, example:
_.finddeep(tree, {id: 5456, parent_id: 555})
tree:
var tree = [ { id: 22, name: 'qqqq', depth: 0, parent_id: 11, children: [ { id: 222, name: 'ttttt', depth: 1, parent_id: 444, children: [], positions: [] }, { id: 5456, name: 'yyyy', depth: 1, parent_id: 555, children: [ { id: 6767, name: 'dfgfdg', depth: 3, parent_id: 6564, children: [], positions: [] }, { id: 4345, name: 'dfgdgfg', depth: 3, parent_id: 45234, children: [], positions: [] }, ], positions: [] }, ], positions: [ { id: 14, name: 'rere', price: 20 }, { id: 12, name: 'tttyty', price: 30 }, ] }, { id: 33, name: 'wwww', depth: 0, parent_id: 22, children: [], positions: [] }, { id: 44, name: 'eeee', depth: 0, parent_id: 33, children: [], positions: [] }, ]
wrong function, alaways returns 'undefined', console.log display founded node:
_.mixin({ finddeep: function(items, attrs) { var key, n_key, n_value, result, value; result = _.findwhere(items, attrs); console.log(items, result, _.isundefined(result)); if (_.isundefined(result)) { (key in items) { value = items[key]; (n_key in value) { n_value = value[n_key]; if (_.isobject(n_value) || _.isarray(n_value)) { result = _.finddeep(n_value, attrs); if (!_.isundefined(result)) { return result; } } } } } return result; } });
where mistake? please me
in code,
for (n_key in value) { n_value = value[n_key]; if (_.isobject(n_value) || _.isarray(n_value)) { _.finddeep(n_value, attrs); } }
is doing deep search, doesn't return result. should assign result search, , if result not undefined
, return or break loop imediately.
so becomes:
_.mixin({ finddeep: function(items, attrs) { var key, n_key, n_value, result, value; result = _.findwhere(items, attrs); console.log(items, result, _.isundefined(result)); if (_.isundefined(result)) { (key in items) { value = items[key]; (n_key in value) { n_value = value[n_key]; if (_.isobject(n_value) || _.isarray(n_value)) { result = _.finddeep(n_value, attrs); } // once find result, can return founded result if (!_.isundefined(result)) { return result; } } } } return result; } });
snippet show correct result:
var tree = [ { id: 22, name: 'qqqq', depth: 0, parent_id: 11, children: [ { id: 222, name: 'ttttt', depth: 1, parent_id: 444, children: [], positions: [] }, { id: 5456, name: 'yyyy', depth: 1, parent_id: 555, children: [ { id: 6767, name: 'dfgfdg', depth: 3, parent_id: 6564, children: [], positions: [] }, { id: 4345, name: 'dfgdgfg', depth: 3, parent_id: 45234, children: [], positions: [] }, ], positions: [] }, ], positions: [ { id: 14, name: 'rere', price: 20 }, { id: 12, name: 'tttyty', price: 30 }, ] }, { id: 33, name: 'wwww', depth: 0, parent_id: 22, children: [], positions: [] }, { id: 44, name: 'eeee', depth: 0, parent_id: 33, children: [], positions: [] }, ]; _.mixin({ finddeep: function(items, attrs) { var key, n_key, n_value, result, value; result = _.findwhere(items, attrs); console.log(items, result, _.isundefined(result)); if (_.isundefined(result)) { (key in items) { value = items[key]; (n_key in value) { n_value = value[n_key]; if (_.isobject(n_value) || _.isarray(n_value)) { result = _.finddeep(n_value, attrs); } // once find result, can return founded result if (!_.isundefined(result)) { return result; } } } } return result; } }); console.log(_.finddeep(tree, {id: 5456, parent_id: 555}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
Comments
Post a Comment