数组转对象
第一种:
var a = ['111','222','333'];let forObject = {}; //forEach循环转对象a.forEach( (item,index) => { forObject[index] = item;}) console.log(forObject);结果:
这里面的item就是value值,index就是key值
第二种:
a.forEach( (item,index) => { // 注意赋值的变化 forObject[item] = item;})console.log(forObject);结果:
第三种:
当数组中的inde值和value不同的时候
var Arraya = [ {a:'tzdnht'}, {b:'rydhm'} ]; let forObject = {}; //forEach循环转对象 Arraya.forEach( (item,index) => { forObject[index] = item; }) console.log(forObject);结果:
第四种:
var Arraya = [ { name:'a', age:12 }, { name:'b', age:26 } ]; let forObject = {}; //forEach循环转对象 Arraya.forEach( (item,index) => { forObject[item.name] = item.age; }) console.log(forObject);结果:
第一种和第四种是我目前用到最多的;