js将数组转化为一个对象obj,键值对形式{}
var formArr = [
0:{name: "Name", value: "111"},
1:{name: "Price", value: "2"},
2:{name: "ProducingArea", value: "3"},
3:{name: "ShelfLife", value: "44"},
4:{name: "Stock", value: "5"}
];
var obj={ };
//map循环方式
formArr.map(function (e, item) {
console.log(e.name);
obj[e.name] = e.value;
});
//$.each循环方式
$.each(formArr, function (item,e) {
console.log(e.name);
obj[e.name] = e.value;
})
//for in 循环方式
for (var item in formArr){
obj[formArr[item].name] = formArr[item].value;
};
结果打印:obj = {Name: "2", Price: "3", ProducingArea: "4", ShelfLife: "5", Stock: "6"}