一段让自己好好理解reduce的代码

const pick = (obj, arr) =>
    arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
const pick = (obj, arr) => arr.reduce((k, v) => {
    //第一个参数为迭代对象第二个为目标对象
    if ((v in obj) && (k[v] = obj[v])) {
        return v, k;
    }
}, {});
console.log(pick({'a': 1, 'b': '2', 'c': 3}, ['a', 'c']));
posted @ 2018-12-18 16:47  猫神甜辣酱  阅读(353)  评论(0编辑  收藏  举报