返回第一个非空/未定义参数

5年以前  |  阅读数:378 次  |  编程语言:JavaScript 

返回第一个非空/未定义参数。

使用Array.find()返回第一个非null/undefined的参数。

const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_))
// coalesce(null,undefined,"",NaN, "Waldo") -> ""

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8