Skip to content

Latest commit

 

History

History
33 lines (20 loc) · 1.03 KB

coalesce.md

File metadata and controls

33 lines (20 loc) · 1.03 KB
标题 标签
coalesce(第一个非 null 和 undefined 的函数) type,beginner(类型,初学者)

返回第一个非null和undefined参数的函数。

  • 使用 Array.prototype.find() 和 Array.prototype.includes() 查找第一个不等于 undefined 或 null 的值。

代码如下:

const coalesce = (...args) => args.find(v => ![undefined, null].includes(v));

ts 代码如下:

调用方式:

coalesce(null, undefined, '', NaN, 'Waldo'); // ''

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/coalesce.html"></iframe>