array to object
array to object
native js & ES6
https://stackoverflow.com/questions/4215737/convert-array-to-object
Object.assign({}, ['a','b','c']);
// {0: "a", 1: "b", 2: "c"}
{ ...['a', 'b', 'c'] }
// {0: "a", 1: "b", 2: "c"}
['a', 'b', 'c'].reduce((o,v,i)=>(o[i]=v,o), {});
// {0: "a", 1: "b", 2: "c"}
const arr = ['a','b','c'];
let obj = {};
obj = Object.assign({}, arr);
// {0: "a", 1: "b", 2: "c"}
obj = { ...arr }
// {0: "a", 1: "b", 2: "c"}
obj = arr.reduce((o,v,i)=>(o[i]=v,o), {});
// {0: "a", 1: "b", 2: "c"}
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/10137310.html
未经授权禁止转载,违者必究!