I have an array, that holds a large number of two-dimensional arrays:
JavaScript
x
8
1
var myArray = [
2
[2260146,2334221,"copy"],
3
[1226218,2334231,"copy"],
4
[2230932,-1,"copy"],
5
[2230933,-1,"copy"],
6
[2230934,-1,"copy"]
7
]
8
I need to convert this array into an object of the following form to send it as JSON:
JavaScript
1
18
18
1
var json = [
2
{
3
"s_id": 2260146,
4
"t_id": 2334221,
5
"type": "copy"
6
},
7
{
8
"s_id": 1226218,
9
"t_id": 2334231,
10
"type": "copy"
11
},
12
{
13
"s_id": 12,
14
"t_id": -1,
15
"type": "copy"
16
}
17
]
18
(“s_id” should be myArray[0][0]
, “t_id myArray[0][1]
, and “type” myArray[0][2]
and so on.)
How can I get the array in the desired form? Thanks in advance.
Advertisement
Answer
JavaScript
1
8
1
json = myArray.map(function(x) {
2
return {
3
"s_id": x[0],
4
"t_id": x[1],
5
"type": x[2]
6
}
7
})
8
Be aware that map is not supported in IEs < 9.