1

i have this two arrays of equal length.

array1 = {a,a,a,b,b,b,b,b,c,c,c,c,c,c,d,d,d,d,e,e}
array2 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}

i want to create an object.

var obj = {
'a': {1,2,3} 
'b': {4,5,6,7,8}
'c': {9,10,11,12,13,14}
 .....
 .....
}

can somebody help me with the logic.

4
  • In the example you are creating two objects for array1, array2, typo? Commented Jul 21, 2011 at 6:53
  • Please consider this as a symbolic array. They are arrays i am sorry for syntax. Commented Jul 21, 2011 at 6:56
  • 1
    Please show us what have you done so far. Where are u stuck Commented Jul 21, 2011 at 6:56
  • 1
    Have you tried to write an algorithm for this? What went wrong? Commented Jul 21, 2011 at 6:57

4 Answers 4

4

Assuming you mean arrays in your example (names + question title):

var combined = {};

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

    if(!(key in combined)) {
        combined[key] = [];
    }

    combined[key].push(array2[i]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Maybe I am missing something, but you define the function "include" and do not call it anywhere.
It's nice that the include function comes from somewhere, but what do you use it for in this example? ;)
Instead of "!key in combined" use !(key in combined)
Oh deary me, I was thinking I needed it and then didn't actually need it. Let's just keep it at the early time in my timezone. I'll edit it out.
3
var group = {};

if (array1.length == array2.length) {
  for (var i=0, j=array1.length; i<j; i++) {
    if ( !(array1[i] in group) ) group[array1[i]] = [];
    group[array1[i]].push(array2[i]);
  }
}

Comments

2
var array1 = ['a','a','a','b','b','b','b','b','c','c','c','c','c','c','d','d','d','d','e','e'];
var array2 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
var obj = {};
for (var i = 0, key; (key = array1[i]); i++) {
  if (!obj[key]) {
    obj[key] = [];
  }
  obj[key].push(array2[i]);
}

Should do the trick.

4 Comments

+1 Nice variation on the for loop, but (!obj[key]) is too sloppy.
Thinking about it, (key = array1[i]) also is too sloppy. What if array1[i] is something that would be a valid object key value but evaluates to false, like the empty string?
This wasn't written for the general case with arbitrary keys and values. I see where you're calling slopiness and it's true, but only for the general case. If he's never going to use a falsy value, why worry about them?
Well, do you know it? All the OP said was "two arrays of equal length" and a generic sample. After all, if you wrapped that up in a function, it would have a bug.
0
var array1 = ['a','a','a','b','b','b','b','b','c','c','c','c','c','c','d','d','d','d','e','e']
var array2 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
var travesedArrayIndex=[];
var Component=function(name, count)
{
  this.name=name;
  this.number=count;
}
var componentList=[];
for(i=0;i<array1.length; i++)
{
  var count=0;
  var valueToBeComparedWith=array1[i];
  for(j=0;j<array1.length;j++)
  {
    if(!AlreadyTraversed(j))
    {
      if(valueToBeComparedWith==array1[j])
      {
        count=count+1;
        travesedArrayIndex.push(j);
      }
    }
  }
  if(count>0)
  {
    var comp= new Component(valueToBeComparedWith,count);
    componentList.push(comp);  
  }
}

 var FinalComponent=function()
 {
  this.item;
  this.numberArray=[];
 }
 var FinalComponentList=[];
 var j=0;
 for(i=0;i<componentList.length;i++)
 {
    var finalComp= new FinalComponent();
    finalComp.item=componentList[i].name;
    var sizeOfArray=componentList[i].number + j;
    for(j;j<sizeOfArray;j++)
    {
      finalComp.numberArray.push(array2[j]);
    }
    FinalComponentList.push(finalComp);
 }
console.log(FinalComponentList);
function AlreadyTraversed(index)
{
  for(k=0;k<travesedArrayIndex.length;k++)
  {
    if(index==travesedArrayIndex[k])
    {
      return true;
    }
  }
  return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.