compack函数
创建一个新数组,包含原数组中所有的非假值元素。
例如false
, null
,0
, ""
, undefined
, 和 NaN
都是被认为是"假值"。
一般用于过滤数组中的假值
在定义返回类型时候,采用Exclude
条件类型把null
、undefiend
、false
、 0
、""
、这几种值排除掉.
/** * * 创建一个新数组,包含原数组中所有的非假值元素。 * 例如false, null,0, "", undefined, 和 NaN 都是被认为是"假值"。 * * * @param array 待处理数组 * @returns ${Array} 返回过滤掉假值的新数组 * @example * * compack(['1',0,'',null,undefined,NaN]) * // => ['1'] */const compack = <T>(array: Array<T>): Array<Exclude<T, null | undefined | false | 0 | "">> => { let result = new Array(); // 边界检查与条件判断 for (let i = 0; i < array.length; i++) { if (array[i]) result.push(array[i]); } return result;};export default compack;
实用例子:
import compack from "../src/compack";const arr = [0, , , "", 2, null, undefined, NaN, "s"];const nw = compack(arr);console.log(nw); // [ 2, 's' ]
个人作品
ip定位查询浏览器插件
老虎优惠券浏览器插件
原文转载:http://www.shaoqun.com/a/506184.html
好卖家:https://www.ikjzd.com/w/776
海豚村:https://www.ikjzd.com/w/1779
compack函数创建一个新数组,包含原数组中所有的非假值元素。例如false,null,0,"",undefined,和NaN都是被认为是"假值"。一般用于过滤数组中的假值在定义返回类型时候,采用Exclude条件类型把null、undefiend、false、0、""、这几种值排除掉./****创建一个新数组,包含原数组中所有的非假值元素。*例如fals
google correlate:google correlate
凹凸曼:凹凸曼
广东会馆 :广东会馆
2018年亚马逊家具类目什么最好卖:2018年亚马逊家具类目什么最好卖
小榄车站去帆鲨海洋坐几路:小榄车站去帆鲨海洋坐几路
No comments:
Post a Comment