javascript - JS 如何改进这个数组 -> 对象函数
问题描述
我正在尝试从我希望用作新对象键的值数组构建一个空数组对象。
const FILTERS = ['foo', 'bar', 'baz']
createFilteringObject() === { foo: [], bar: [], baz: []}
function createFilteringObject() {
const filterObj = {}
for (const filterCategory of FILTERS) {
filterObj[filterCategory] = [];
}
return filterObj;
}
我想提高我的 ES6 知识。有没有人有一种巧妙的方法可以在一行中做到这一点?
解决方案
FILTERS.reduce((a,b) => Object.assign(a, { [b]: []}), {})