碎片时间学编程「349]:根据函数从左侧删除数组元素

删除数组中的元素,直到传递的函数返回 true为值。返回数组中的剩余元素。 循环遍历数组,使用 Array.prototype.slice() 方法删除数组的第一个元素,直到 func 返回的值为 true。 返回剩余元素。
const dropWhile = (arr, func) => { while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); return arr;};
示例:
dropWhile([1, 2, 3, 4], n => n >= 3); // [3, 4]
更多内容请访问我的网站:https://www.icoderoad.com