2013-10-02 96 views
2

我有國家和其他陣列與產品名稱的源數組。這兩個陣列的目標是平等的。但是我不能正確地分組數組。我總是得到一個結果數據沒有正確分組

{{China, {Lenovo}},{China, {Asus}} ... }. 

我的代碼是:

for (i = 0; i < XArray.length; i++) { 
    var pointsArray = []; 

    for (j = 0; j < XArray.length; j++) { 

     if (XArray[i] == XArray[j]) { 
      pointsArray.push([parseFloat(YArray[i]), parseFloat(ZArray[i])]); 
     } 
    } 

    dataSource.push({ 
     name: i, 
     data: pointsArray.slice() 
    }); 
    pointsArray.length = 0; 
} 
+2

有爲什麼要使用2個陣列,而不是一個JSON對象中的任何特別的原因? – jeff

+0

如果美國那麼陣列應該是:USA {Apple,Blackberry} right –

+0

真的想要:'[{中國:[聯想,華碩]},{韓國:[三星]} ...''? – RobG

回答

4

我認爲你應該按國家使用對象組製造商。就像這樣:

var conutries, 
    manufacturers, 
    i, 
    len, 
    country, 
    manufacturerByCountry; 
countries = ['China', 'China', 'Korea', 'USA', 'USA']; 
manufacturers = ['Lenovo', 'Asus', 'Samsung', 'Apple', 'Blackberry']; 
len = countries.length; 
manufacturerByCountry = {}; 
// expected result: 
// array : {'China': ['Lenovo', 'Asus'], 'Korea': ['Samsung'] ... } 

for(i = 0; i < len; i++) { 
    country = countries[i]; 
    if(!manufacturerByCountry[country]) { 
     manufacturerByCountry[country] = []; 
    } 
    manufacturerByCountry[country].push(manufacturers[i]); 
} 

console.log(manufacturerByCountry); 

DEMO


如果您仍然希望得到您的問題所描述的結果,那麼你可以通過這樣的一種解決方案:

var conutries, 
    manufacturers, 
    i, 
    len, 
    country, 
    manufacturerByCountry, 
    countryIndex, 
    index; 
countries = ['China', 'China', 'Korea', 'USA', 'USA']; 
manufacturers = ['Lenovo', 'Asus', 'Samsung', 'Apple', 'Blackberry']; 
len = countries.length, 
manufacturerByCountry = [], 
countryIndex = {}; 
// expected result: 
// array : [[China, [Lenovo, Asus]], [Korea, [Samsung]] ... ] 

for(i = 0; i < len; i++) { 
    country = countries[i]; 
    if(countryIndex[country] === undefined) { 
     index = manufacturerByCountry.push([country, []]) - 1; 
     countryIndex[country] = index; 
    } else { 
     index = countryIndex[country]; 
    } 
    manufacturerByCountry[index][1].push(manufacturers[i]); 
} 

console.log(manufacturerByCountry); 

DEMO

+1

感謝您的解決方案。它適合我! –

0

不太確定的語法,但我認爲你正在嘗試要做到這一點:

var array1 = ['China', 'China', 'Korea', 'USA', 'USA']; 
var array2 = ['Lenovo', 'Asus', 'Samsung', 'Apple', 'Blackberry']; 
var result = []; 
var name, names = {}; 
var nameCount = 0; 
var obj; 

for (var i=0, iLen=array1.length; i<iLen; i++) { 
    name = array1[i]; 

    // If haven't found name already, setup structure 
    // so value can be added after the block 
    if (!(name in names)) { 

    // Add name to the list of names and remember its index 
    names[name] = nameCount; 

    // Create a new object 
    obj = {}; 

    // Give it a property with value of name and assign an array 
    obj[name] = []; 

    // Add it to results 
    result.push(obj); 

    // Increment index for next time 
    ++nameCount; 
    } 

    // Add member of array2 to name object's array 
    result[names[name]][name].push(array2[i]); 
} 

它創建類似對象的數組:

[ 
    {China: ['Lenovo', 'Asus']}, 
    {Korea: ['Samsung']}, 
    {USA: ['Apple', 'Blackberry']} 
]