我有一组数据.在一个有10个字段,在b有10个字段
var a = [ "siddharth","sid","anything","something","nothing",] var b = [ "23","67","10","90" ]
我试图从这些数组中创建一个JSON作为键和b
作为值,如下图所示:
{ "siddharth" : "23","sid" : "67" }
我如何使用javascript或jquery来实现这一点.我现在的代码是
var convert = '{'+datatest.columnHeaders[i].name +":"+datatest.rows[0][i]+'}'; pair = convert;/*JSON.stringify(convert);*/ array.pairArray.push(pair);
解决方法
假设两个数组总是相同的长度:
var obj = {} for (var i = 0; i < a.length; i++) { //or check with: if (b.length > i) { assignment } obj[a[i]] = b[i] }