2016-11-28 67 views
1

我想將兩個數組轉換爲另一種數組格式。數組轉換給出了字符串而不是數組

這裏是我的代碼:

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}, 
]; 
+0

KOTAK從哪裏來?是不是你想成爲加爾各答的輸出? –

+0

對不起Tirthraj Barot。那就是「加爾各答」。我錯誤地給出了這樣的結果 –

+0

此外,根據我的觀點,「Kotak」:25等任何值都必須是一個對象內的對象......所以根據我認爲的有效性,我將代碼放在下面。 –

回答

2

您可以迭代array2並映射每個條目的新對象。

var array1 = ["Kolkata", "Begumpet", "Bellary"], 
 
    array2 = [[[20, "Kolkata1"], [10, "Kolkata2c"]], [[0, "Begumpet1"], [10, "Begumpet2e"]], [[30, "Bellary1"], [0, "Bellary2a"]]], 
 
    dataset = array2.map(function (a, i) { 
 
     var o = { label: array1[i] }; 
 
     a.forEach(function (b) { 
 
      o[b[1]] = b[0]; 
 
     }); 
 
     return o; 
 
    }); 
 

 
console.log(dataset);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

謝謝@Nina Scholz –

0

要轉換爲JavaScript對象到JSON你必須調用JSON.stringify方法。

var jsonString = JSON.stringify(array2); 
+0

您似乎沒有閱讀過問題標題。 OP實際上是在詢問如何將數據轉換爲對象數組。 – Quentin

+0

嗨Cubi,我想將字符串轉換爲JSON,但我不想讓json將字符串轉換爲 –

1

另一種解決方案使用Array.prototype.reduceforEach在內陣列以獲得所需的陣列 - 以下演示:

var array1 = [ "Kolkata", "Begumpet", "Bellary"]; 
 
var array2 = [[[20,"Kolkata1"],[10,"Kolkata2c"]],[[0,"Begumpet1"],[10, "Begumpet2e"]],[[30, "Bellary1"],[0, "Bellary2a"]]]; 
 

 
var result = array2.reduce(function(p,c,i){ 
 
    var elem = {label : array1[i]}; 
 
    c.forEach(e => elem[e[1]] = e[0]); 
 
    p.push(elem); 
 
    return p; 
 
},[]); 
 

 
console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

1

你可以用做這個ES6代碼:

var array1 = [ "Kolkata", "Begumpet", "Bellary"]; 
 
var array2 = [[[20,"Kolkata1"],[10,"Kolkata2c"]], 
 
       [[0,"Begumpet1"],[10, "Begumpet2e"]],[[30, "Bellary1"],[0, "Bellary2a"]]] 
 

 
var result = array2.map((pairs, i) => 
 
    Object.assign ({ label: array1[i] }, ...pairs.map (([v, k]) => ({ [k]: v })))); 
 
    
 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

謝謝@trincot –

+0

不客氣。請注意,你只能接受一個答案(看起來你試圖接受多個答案)。 – trincot

+1

嗨trincot。我可以同意,但每個答覆都是正確的,爲什麼我困惑。但我學到更多。每個人的思維方式都不一樣。所以我同意所有的迴應。感謝你們。非常感謝。 –

1

有根據JSON的結構的最佳做法所需輸出JSON對象一些修正。所以根據我認爲是有效的,這裏是正確的代碼。

此外,我用JSON.stringify()來打印JSON對象。

而且我還修改了構建輸出JSON對象的方式。

var array1 = ["Kolkata", "Begumpet", "Bellary"]; 
 

 
var array2 = [ 
 
    [ 
 
    [20, "Kolkata1"], 
 
    [10, "Kolkata2c"] 
 
    ], 
 
    [ 
 
    [0, "Begumpet1"], 
 
    [10, "Begumpet2e"] 
 
    ], 
 
    [ 
 
    [30, "Bellary1"], 
 
    [0, "Bellary2a"] 
 
    ] 
 
]; 
 

 
var resultvalue = []; 
 
for (i = 0; i < array1.length; i++) { 
 
    var result = {}; 
 
    var temp = {}; 
 
    temp.label = array1[i]; 
 
    for (j = 0; j < 2; j++) { 
 
    temp[[array2[i][j][1]]] = array2[i][j][0]; 
 
    } 
 
    resultvalue.push(temp); 
 
} 
 

 
document.write(JSON.stringify(resultvalue)); 
 
console.log(resultvalue);

SORRY ....不明白的問題......我更新的代碼。