-3

I need to regroup these two arrays:

arrayOne = ['A', 'B', 'C', 'D', 'A', 'C'];
arrayTwo = ['1', '2', '3', '4', '5', '6'];

Here's the output I'm looking for:

groupedOutput = {
  'A': ['1', '5'],
  'B': ['2'],
  'C': ['3', '6'],
  'D': ['4']
}

Any ideas?

Chris Spittles
  • 15,023
  • 10
  • 61
  • 85
Server Khalilov
  • 408
  • 2
  • 5
  • 20
  • something like this maybe: http://stackoverflow.com/questions/5667888/counting-occurences-of-javascript-array-elements – user12733 Jul 15 '14 at 15:28
  • Andy, I need to match the indexes of both arrays. As a result, I want to have two-dimensional array, where: `element[0][0]='A'; element[0][1]=['1','5']; element[1][0]='B'; element[1][1]=['2']; element[2][0]='C'; element[2][1]=['3','6']; element[3][0]='D'; element[3][1]=['4'];` – Server Khalilov Jul 15 '14 at 15:36

2 Answers2

2
var groupedObj = {};
for(var i = 0; i< arrayOne.length; i ++) {
    if (typeof groupedObj[arrayOne[i]] === "undefined") groupedObj[arrayOne[i]] = [];
    groupedObj[arrayOne[i]].push(arrayTwo[i]);
}

groupedObj will contain your desired result

0

here is the code(but I left somethings for you to do):

var aar = [][]
for(var i = 0 ; i<arrayone.length ; i++)
{
    arr[i][0] = arrayone[i];
    arr[i][1] = arraytwo[i];
}
for(var i = 0 ; i<arrayone.length ; i++)
{
    for(var j = i ; j<arrayone.length ; j++)
    {
        if(arr[i] == arr[j])
        {
           arr[i][1] = arr[i][1] + " , " + arr[j][1];
        }
    }
}
Lrrr
  • 4,755
  • 5
  • 41
  • 63