UNPKG

vtils

Version:

一个面向业务的 JavaScript/TypeScript 实用程序库。

28 lines (26 loc) 804 B
/** * 构造条件数组,即允许一个数组的项出现 false 值,且最终 false 值的项目会被排除。 * * @param array 数组 * @param childrenKey 子数组键 */ /** * 构造条件数组,即允许一个数组的项出现 false 值,且最终 false 值的项目会被排除。 * * @param array 数组 */ export function makeConditionalArray(array, childrenKey) { return array.filter(function filter(item) { if (item && childrenKey && Array.isArray(item[childrenKey])) { item[childrenKey] = item[childrenKey].filter(filter); } return !!item; }); } function makeConditionalArrayFork(childrenKey) { // @ts-ignore return function (array) { return makeConditionalArray(array, childrenKey); }; } makeConditionalArray.fork = makeConditionalArrayFork;