将两个数组合并成一个Json对象。

时间:2021-07-05 12:18:18

I have an array of data. In a there are 10 fields and in b there are 10 fields

我有一个数据数组。a有10个字段,b有10个字段

var a = [ "siddharth", "sid", "anything", "something", "nothing", ]
var b = [ "23", "67", "10", "10", "90" ]

I am trying to create a JSON from these arrays as a as the key and b as values, as shown below:

我正在尝试从这些数组中创建一个JSON, a作为键,b作为值,如下所示:

{  "siddharth" : "23",  "sid" : "67" }

How can I achieve this using javascript or jquery. My current code is

如何使用javascript或jquery实现这一点?我现在的代码是

 var convert = '{'+datatest.columnHeaders[i].name +":"+datatest.rows[0][i]+'}';
         pair   = convert;/*JSON.stringify(convert);*/
         array.pairArray.push(pair);

8 个解决方案

#1


14  

Assuming both arrays are always the same length:

假设两个数组总是相同的长度:

var obj = {}
for (var i = 0; i < a.length; i++) {
    //or check with: if (b.length > i) { assignment }
    obj[a[i]] = b[i]
}

#2


2  

var a = [ "siddharth", "sid", "anything", "something", "nothing" ];
var b = [ "23", "67", "10", "10", "90" ];

var c = {};
$.each( a, function(i,v) {
  c[ v ] = b[ i ];
});

$('body').append( JSON.stringify(c) );
//Output: {"siddharth":"23","sid":"67","anything":"10","something":"10","nothing":"90"}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

#3


2  

This example is equal to tymeJVs example, but uses forEach loop for array. For me it looks shorter.

此示例与tymeJVs示例相同,但对数组使用forEach循环。对我来说,它看起来更短。

var obj = {};
a.forEach(function(item, i) {
    obj[item] = b[i];
});
console.log(JSON.stringify(obj)); // {"siddharth":"23","sid":"67","anything":"10","something":"10","nothing":"90"}

#4


1  

You would need quotation marks around the name and the value, otherwise you end up with a string like {siddharth:23,sid:67}:

您需要在名称和值周围加上引号,否则您将得到一个字符串,如{siddharth:23,sid:67}:

// mock the data
var datatest = {
  columnHeaders: [ { name: "siddharth" }, { name: "sid" }, { name: "anything" }, { name: "something" }, { name: "nothing" } ],
  rows: [[ "23", "67", "10", "10", "90" ]]
};

var json = '{';
for (var i = 0; i < datatest.columnHeaders.length; i++) {
  if (i > 0) json += ',';
  json += '"' + datatest.columnHeaders[i].name + '":"' + datatest.rows[0][i]  +'"';
}
json += '}';

// show result in * snippet
document.write(json);

#5


1  

You can create plain object and use it as the mapping container:

您可以创建普通对象并将其用作映射容器:

var a = [ "siddharth", "sid", "anything", "something", "nothing" ];
var b = [ "23", "67", "10", "10", "90" ];
var obj = {};
for ( i = 0; i < a.length; i++) {
    obj[a[i]] = b[i];
}
alert(JSON.stringify(obj));

Please refer to How to create a hash or dictionary object in JavaScript for more information

有关更多信息,请参考如何在JavaScript中创建散列或字典对象

#6


1  

Shortest i can get using ES6 fat arrow function, map, reduce & computed property names

使用ES6 fat箭头函数、映射、reduce和计算属性名可以获得的最短属性

var a = ["siddharth", "sid", "anything", "something", "nothing", ]
var b = ["23", "67", "10", "10", "90"]

const mergeArrToJSON = (a, b) => a
    .map((item, i) => ({[item]: b[i]})) 
    .reduce((json,val)=>Object.assign({},json,val))

console.log(mergeArrToJSON(a, b))

#7


0  

If you are willing to add another famous third-party javascript utility library

如果您愿意添加另一个著名的第三方javascript实用程序库

Lodash, use zipObject:

使用zipObject Lodash:

_.zipObject(a, b);

Underscore, use object:

强调,使用对象:

_.object(a, b);

#8


0  

If you are free to use additional plugin then

如果你可以*使用其他插件

UNDERSCORE

下划线

is what you are looking for

你在找什么?

_.object(['moe', 'larry', 'curly'], [30, 40, 50]);
=> {moe: 30, larry: 40, curly: 50}

Single line code. Just include this js

一行代码。包括这个js

https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js

#1


14  

Assuming both arrays are always the same length:

假设两个数组总是相同的长度:

var obj = {}
for (var i = 0; i < a.length; i++) {
    //or check with: if (b.length > i) { assignment }
    obj[a[i]] = b[i]
}

#2


2  

var a = [ "siddharth", "sid", "anything", "something", "nothing" ];
var b = [ "23", "67", "10", "10", "90" ];

var c = {};
$.each( a, function(i,v) {
  c[ v ] = b[ i ];
});

$('body').append( JSON.stringify(c) );
//Output: {"siddharth":"23","sid":"67","anything":"10","something":"10","nothing":"90"}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

#3


2  

This example is equal to tymeJVs example, but uses forEach loop for array. For me it looks shorter.

此示例与tymeJVs示例相同,但对数组使用forEach循环。对我来说,它看起来更短。

var obj = {};
a.forEach(function(item, i) {
    obj[item] = b[i];
});
console.log(JSON.stringify(obj)); // {"siddharth":"23","sid":"67","anything":"10","something":"10","nothing":"90"}

#4


1  

You would need quotation marks around the name and the value, otherwise you end up with a string like {siddharth:23,sid:67}:

您需要在名称和值周围加上引号,否则您将得到一个字符串,如{siddharth:23,sid:67}:

// mock the data
var datatest = {
  columnHeaders: [ { name: "siddharth" }, { name: "sid" }, { name: "anything" }, { name: "something" }, { name: "nothing" } ],
  rows: [[ "23", "67", "10", "10", "90" ]]
};

var json = '{';
for (var i = 0; i < datatest.columnHeaders.length; i++) {
  if (i > 0) json += ',';
  json += '"' + datatest.columnHeaders[i].name + '":"' + datatest.rows[0][i]  +'"';
}
json += '}';

// show result in * snippet
document.write(json);

#5


1  

You can create plain object and use it as the mapping container:

您可以创建普通对象并将其用作映射容器:

var a = [ "siddharth", "sid", "anything", "something", "nothing" ];
var b = [ "23", "67", "10", "10", "90" ];
var obj = {};
for ( i = 0; i < a.length; i++) {
    obj[a[i]] = b[i];
}
alert(JSON.stringify(obj));

Please refer to How to create a hash or dictionary object in JavaScript for more information

有关更多信息,请参考如何在JavaScript中创建散列或字典对象

#6


1  

Shortest i can get using ES6 fat arrow function, map, reduce & computed property names

使用ES6 fat箭头函数、映射、reduce和计算属性名可以获得的最短属性

var a = ["siddharth", "sid", "anything", "something", "nothing", ]
var b = ["23", "67", "10", "10", "90"]

const mergeArrToJSON = (a, b) => a
    .map((item, i) => ({[item]: b[i]})) 
    .reduce((json,val)=>Object.assign({},json,val))

console.log(mergeArrToJSON(a, b))

#7


0  

If you are willing to add another famous third-party javascript utility library

如果您愿意添加另一个著名的第三方javascript实用程序库

Lodash, use zipObject:

使用zipObject Lodash:

_.zipObject(a, b);

Underscore, use object:

强调,使用对象:

_.object(a, b);

#8


0  

If you are free to use additional plugin then

如果你可以*使用其他插件

UNDERSCORE

下划线

is what you are looking for

你在找什么?

_.object(['moe', 'larry', 'curly'], [30, 40, 50]);
=> {moe: 30, larry: 40, curly: 50}

Single line code. Just include this js

一行代码。包括这个js

https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js