实例1 用map循环遍历
var formArr=[
{name:"soina",value:'122'},
{name:"amy",value:'123'},
{name:"foina",value:'124'},
];
var obj={}; //定义一个空对象
//map循环方式
formArr.map(function (e,item) {
console.log(e.name); //拿到key对应的值
obj[e.name]=e.value; //{soina: "122", amy: "123", foina: "124"}
});
实例2 用$.each实现
var formArr2=[
{name:"soina",value:'122'},
{name:"amy",value:'123'},
{name:"foina",value:'124'},
];
var obj2={}; //定义一个空对象
$.each(formArr2,function(item, e) {
obj2[e.name]=e.value;
});
console.log(obj2); //{soina: "122", amy: "123", foina: "124"}
实例3 用for in 循环方式实现
var formArr3=[
{name:"soina",value:'122'},
{name:"amy",value:'123'},
{name:"foina",value:'124'},
];
var obj3={}; //定义一个空对象
for(var item in formArr3 ){
obj3[formArr3[item].name]=formArr3[item].value;
}
console.log(obj3);