源数据:config:{ paper:{
id:require('tools/rand-str')(),
title:'-',
selector:['-','-','-'],
val:1
}, paint:{ id:require('tools/rand-str')(),
title:'-',
selector:['-','-'],
val:1
} optimize:{ id:require('tools/rand-str')(),
title:'-',
emphasis:true,
selector:['-','-'],
val:0
}
}目标数据:val:{ paper:1,
paint:1,
optimize:0}id:{ paper:'rXTUtK',
paint:'r8LizN',
optimize:'rfIpES'}
1 回答

汪汪一只猫
TA贡献1898条经验 获得超8个赞
哪里需要用 lodash,原生脚本就解决了
const
和 Lambda 是 ES6 的语法,如果需要 ES5 你可以自己
把
const
换成var
把 Lambda 替换成 function
const result = Object.keys(config).reduce((r, c) => { const options = config[c]; ["val", "id"].forEach(k => { (r[k] = r[k] || {})[c] = options[k]; }); return r; }, {});
如果需要所有属性,而不只是 val
和 id
,可以把 ["val", "id"]
换成 Object.keys(options)
。
如果用 lodash,大概会用到它的 transform
,其实和原生的 reduce
也差不多。
添加回答
举报
0/150
提交
取消