高阶函数:数组转哈希对象

直接上方法

const hashObj = (arr) => arr.reduce((hash,{title,...keys}) => {
        hash[title] = keys;
        return hash
    },
    {}
);
var colors = [
    {
        id: 'xa',
        title: 'xaTit',
        rating: '91'
    },
    {
        id: 'xb',
        title: 'xbTit',
        rating: '92'
    },
    {
        id: 'xc',
        title: 'xcTit',
        rating: '93'
    },
    {
        id: 'xd',
        title: 'xdTit',
        rating: '94'
    }
]
var a = hashObj(colors);
console.log(a);

a的结果,自行打印看结果。
是不是很神奇!
重点 {key, ...keys}= {x:1,y:2,z:3,key:'value'}

猜你喜欢

转载自blog.csdn.net/aizhemeilishen/article/details/84873429