js高级技巧

将数组中的空值去除

const arrContainsEmptyVal = [3, 4, 5, 2, 3, undefined, null, 0, “”];

将数组中重复项清除

const uniq = arr => […new Set(arr)];

取出对象中的深层属性

1
2
3
4
const pluckDeep = path => obj =>
path.split(".").reduce((val, attr) => val[attr], obj);

pluckDeep("a.b.c")(deepAttr);

将 Stark 家族成员提取出来。注意,目标数据在数组前面,使用 filter 方法遍历整个数组是浪费。

1
2
3
4
5
6
const takeWhile = f => ([head, ...tail]) =>
f(head) ? [head, ...takeWhile(f)(tail)] : [];

const isStark = name => name.toLowerCase().includes("stark");

takeWhile(isStark)(houses);
请支持我一下吧.