obj = {'a':['hello', 'hie'], 'b':['World', 'India']}
到
array = [{'a':'hello','b':'World'}, {'a':'hie','b':'India'}]
使用JQuery为此转换转换此内容或任何内置方法的最佳方法。
答案 0 :(得分:2)
试试此代码,
obj = {'a':['hello', 'hie'], 'b':['World', 'India']}
var key = Object.keys(obj);
array = [];
for (var i = 0; i < obj['a'].length; i++){
o = {}
for (k in key){
o[key[k]] = obj[key[k]][i]
}
array.push(o)
}
答案 1 :(得分:1)
var obj = {'a':['hello', 'hie'], 'b':['World', 'India']};
var array = [];
for (var prop in obj)
for (var i=0; i<obj[prop].length; i++) {
var o = array[i] || (array[i] = {});
o[prop] = obj[prop][i];
}
不需要jQuery。使用jQuery,它可能看起来像这样:
var array = [];
$.each(obj, function(prop) {
$.each(this, function(i) {
var o = array[i] || (array[i] = {});
o[prop] = this;
});
});