2013-10-02 6 views
2

국가 이름 및 제품이있는 다른 어레이가있는 소스 배열이 있습니다. 두 배열의 지표는 동일합니다.데이터가 올바르게 그룹화되지 않았습니다.

array1:{China, China, Korea, USA, USA}, 

array2:{Lenovo, Asus, Samsung, Apple, Blackberry}, 

result : array : {{China, {Lenovo, Asus}}, {Korea, {Samsung}} ... } 

하지만 그룹 배열을 올바르게 사용할 수 없습니다.

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

를? – jeff

+0

미국의 경우 배열은 다음과 같아야합니다. 미국 {Apple, Blackberry} right –

+0

정말 원하실까요? [China : [Lenovo, Asus]}, {Korea : [Samsung]} ...? – RobG

답변

4

난 당신이 국가 그룹 제조 업체에 개체를 사용한다고 생각 : 나는 항상

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

내 코드는 결과를 얻을. 이처럼 : 당신은 아직도 당신이 질문에 설명 한 결과를 얻고 싶다면

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]); 
} 

: 당신이 개 배열 대신 JSON 객체를 사용하는 이유는 특별한 이유가

[ 
    {China: ['Lenovo', 'Asus']}, 
    {Korea: ['Samsung']}, 
    {USA: ['Apple', 'Blackberry']} 
] 
관련 문제