2016-12-06 137 views
1

我在將數組類對象轉換爲對象數組時遇到了一些麻煩。我正在使用reduce方法,它正確地將數組數據轉換爲第一組數組的對象,但第二次通過時,它將數據正確地設置爲key:value對,但不會將其放置在I希望它是在將數組對象的多深度數組轉換爲對象數組

這裏是陣列和功能:

var array = [ 
       [ 
       ['Name', 'Arusha'], ['Species', 'Arabica'], ['Region', 'Mount Meru in Tanzania, and Papua New Guinea'], ['Comments', 'Typica variety or a French Mission'] 
       ], 
       [ 
       ['Name', 'Catimor'], ['Species', 'Interspecific hybrid'], ['Region', 'Latin America'], ['Comments', 'This is cross between Timor coffee and Caturra coffee. It was created in Portugal in 1959'] 
       ] 
      ]; 

function convertArrayToObject(array) { 
    var arr = []; 

    array.reduce(function(result, currentArray) { 
     for (var j = 0, i = 0; i < array[0].length; i++) { 
     result[currentArray[i][0]] = currentArray[i][1]; 
     } 
     arr.push(result); 
     return arr; 
    }, {}); 
return arr; 
} 

我的預期輸出是假設是這個樣子:

[ 
{ Name: 'Arusha', Species: 'Arabica', Region: 'Mount Meru in Tanzania, and Papua New Guinea', Comments: 'Typica variety or a French Mission' }, 
{ Name: 'Catimor', Species: 'Interspecific hybrid', Region: 'Latin America', Comments: 'This is cross between Timor coffee and Caturra coffee. It was created in Portugal in 1959' } 
] 

這就是我目前返回:

[ { Name: 'Arusha', Species: 'Arabica', Region: 'Mount Meru in Tanzania, and Papua New Guinea', Comments: 'Typica variety or a French Mission' }, 
[Circular], 
Name: 'Catimor', 
Species: 'Interspecific hybrid', 
Region: 'Latin America', 
Comments: 'This is cross between Timor coffee and Caturra coffee. It was created in Portugal in 1959' ] 

任何幫助將不勝感激!

+1

您正在尋找'地圖()' - >'reduce()' –

+1

可能是lodash _flatMap會幫你 –

+0

啊!我以爲你只是說使用地圖,但使用地圖,然後減少一定會做到這一點!謝謝! –

回答

4

您可以使用Array#map併爲外部數組的每個元素返回一個新對象。在裏面,你可以迭代所有項目並構建新的屬性。

var array = [[['Name', 'Arusha'], ['Species', 'Arabica'], ['Region', 'Mount Meru in Tanzania, and Papua New Guinea'], ['Comments', 'Typica variety or a French Mission']], [['Name', 'Catimor'], ['Species', 'Interspecific hybrid'], ['Region', 'Latin America'], ['Comments', 'This is cross between Timor coffee and Caturra coffee. It was created in Portugal in 1959']]], 
 
    result = array.map(function (a) { 
 
     var o = {}; 
 
     a.forEach(function (b) { 
 
      o[b[0]] = b[1]; 
 
     }); 
 
     return o; 
 
    }); 
 

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

3

使用Array#map迭代的陣列,且Array#reduce每個子陣列的對象:

array = [[['Name', 'Arusha'], ['Species', 'Arabica'], ['Region', 'Mount Meru in Tanzania, and Papua New Guinea'], ['Comments', 'Typica variety or a French Mission']], [['Name', 'Catimor'], ['Species', 'Interspecific hybrid'], ['Region', 'Latin America'], ['Comments', 'This is cross between Timor coffee and Caturra coffee. It was created in Portugal in 1959']]]; 
 

 
var result = array.map(function(arr) { 
 
    return arr.reduce(function(obj, tuple) { 
 
    obj[tuple[0]] = tuple[1]; 
 
    
 
    return obj; 
 
    }, {}); 
 
}); 
 

 
console.log(result);

+0

這是另一個很好的方法來實現這一點,它仍然包含我最初堅持使用的減少。謝謝! –