I have 2 javascript arrays that I need to convert to a specific format, the current format of those 2 arrays is:
"diavalor": [
"15",
"27",
"44",
"63",
"-1",
"8",
"9",
"22"
],
"diafecha": [
"2018-08-30",
"2018-08-30",
"2018-08-30",
"2018-08-30",
"2018-08-30",
"2018-08-30",
"2018-08-30",
"2018-08-30"
]
and the format I need is:
var data = [
[1, 20],
[2, 40],
[3, 25],
[4, 45],
[5, 25],
[6, 50],
[7, 35],
[8, 60],
[9, 30]
];
and I don't know how to get to said format, I hope you can help me
Update my problem is in the format since what I do:
var array=respuesta.humedad.diavalor;
for (i = 0; i < array.length; i++) {
data[i]=[[it,parseInt(array[i])]];
it+=1;
}
console.log(data);
console.log(data2);
Try something like this.
This is the result in console
This is a pretty short form using
.map()
. By the way you can cast the strings in the first array as integers by adding+
if you want.This is assuming that the relationship between the two arrays is in the order of the arrays themselves, and not in something external. In this case the implementation would obviously be different.