1.数组转对象
展开运算符 (...)
这是一个简单快速的方法
const arr = ['one','two','three'];
const obj = {...arr}; console.log(obj); // { 0: 'one', 1: 'tow', 2: 'three' }
Objcet.assign(target, ...sources)
2.foreach
const classList = {};
classListItem.forEach((item) => {
classList[item.id] = item.tagName;
});
3.reduce
const valueEnumBase = baseList.reduce((p, c) => Object.assign(p, { [c.symbol]: c.symbol }), {})
方法还有很多,这几种是常见的。