多重数组转换对象?
arr = [ {1:"a"},{2:"b"}]
大概就是上边这种数组,怎么转换成下边这种对象。
obj = {1:"a",2:"b"}
向后台传参数的话,是不是下边这种更合适一些?
jquery javascript对象 JavaScript javascript数组 类型转换
amonn
9 years, 3 months ago
Answers
var arr = [ {1:"a"},{2:"b"}];
var obj = {};
//es2015
arr.forEach(function(v){
Object.assign(obj, v);
});
console.log(obj);
var arr = [ {1:"a"},{2:"b"}];
var obj = {};
//jQuery version
jQuery.each(arr,function(k, v){
jQuery.extend(obj, v);
});
console.log(obj);
撸出一个大明湖
answered 9 years, 3 months ago