index.js
865 Bytes
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.arrayTreeFilter = factory());
}(this, (function () { 'use strict';
function arrayTreeFilter(data, filterFn, options) {
options = options || {};
options.childrenKeyName = options.childrenKeyName || "children";
var children = data || [];
var result = [];
var level = 0;
do {
var foundItem = children.filter(function (item) {
return filterFn(item, level);
})[0];
if (!foundItem) {
break;
}
result.push(foundItem);
children = foundItem[options.childrenKeyName] || [];
level += 1;
} while (children.length > 0);
return result;
}
return arrayTreeFilter;
})));