我想將兩個數組轉換爲另一種數組格式。數組轉換給出了字符串而不是數組
這裏是我的代碼:
var array1 = [ "Kolkata", "Begumpet", "Bellary"];
var array2 = [[[20,"Kolkata1"],[10,"Kolkata2c"]],
[[0,"Begumpet1"],[10, "Begumpet2e"]],
[[30, "Bellary1"],[0, "Bellary2a"]]]
console.log(array2);
var resultvalue = [];
for (i = 0; i < array2.length; i++) {
var result = "";
var m = 0;
for (j = 0; j < array2[i].length; j++) {
if(m==0){
result += "label: '" + array1[i] + "',";
m++;
}
result += " '" + array2[i][j][1] + "': " + array2[i][j][0] + ", ";
}
resultvalue.push(result);
}
console.log(resultvalue);
它產生我想要的東西,但結果是一個字符串,而我需要實際的陣列。
預期的輸出對象應該是這樣的:
dataset = [
{label:"Kolkata", "Kolkata1":20, "Kolkata2c":10},
{label:"Begumpet", "Begumpet1":0, "Begumpet2e":10},
{label:"Bellary", "Bellary1":30, "Bellary2a":0},
];
KOTAK從哪裏來?是不是你想成爲加爾各答的輸出? –
對不起Tirthraj Barot。那就是「加爾各答」。我錯誤地給出了這樣的結果 –
此外,根據我的觀點,「Kotak」:25等任何值都必須是一個對象內的對象......所以根據我認爲的有效性,我將代碼放在下面。 –