xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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"}

posted @ 2018-12-18 15:18  xgqfrms  阅读(326)  评论(0编辑  收藏  举报