JSFiddle - React, Tailwind, and code Playground
by f0t0n
JavaScript
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);