Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you should use an object to group manufacturers by country. Like this:</p> <pre><code>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 &lt; len; i++) { country = countries[i]; if(!manufacturerByCountry[country]) { manufacturerByCountry[country] = []; } manufacturerByCountry[country].push(manufacturers[i]); } console.log(manufacturerByCountry); </code></pre> <p><a href="http://jsfiddle.net/f0t0n/gdnRy/" rel="nofollow"><strong>DEMO</strong></a></p> <hr> <p>If you still want to get the result you've described in question, then you can go with the solution like this:</p> <pre><code>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 &lt; 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); </code></pre> <p><a href="http://jsfiddle.net/f0t0n/662JZ/" rel="nofollow"><strong>DEMO</strong></a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload