0

I have two javascript arrays object like below

First array:

count_array[0].left= 0;
count_array[0].width= 33;
count_array[0].id= 1;
count_array[1].left= "";
count_array[1].width= "";
count_array[1].id= 2;
count_array[2].left= "";
count_array[2].width= "";
count_array[2].id= 3;

Second array:

temp_array[0].left= "";
temp_array[0].width= "";
temp_array[0].id= 4;
temp_array[1].left= "";
temp_array[1].width= "";
temp_array[1].id= 5;
temp_array[2].left= 0;
temp_array[2].width= 33;
temp_array[2].id= 1;

I want to get array which has unique value of id in javascript from above two array like below

Third array:

temp_array_cnt[0].left= 0;
temp_array_cnt[0].width= 33;
temp_array_cnt[0].id= 1;

I am using below code but it is not working

function intersection(x,y){
  x.sort();y.sort();
 var i=j=0;ret=[];
 while(i<x.length && j<y.length){
  if(x[i]<y[j])i++;
  else if(y[j]<x[i])j++;
  else {
    ret.push(x[i]);
    i++,j++;
   }
  }
 return ret;

}

Please help me here. Thanks

I am using below function now

   function intersection(x,y){
ret=[];
//x.sort();y.sort();
for(var i=0;i<x.length;i++)
{
 for(var j=0;j<y.length;i++)
 {
    //console.log(x[i].eventid);
    var chk_left=x[i];
    if(chk_left.id == chk_left.id)
    {
        ret.push(chk_left);
    }
 }
}
   return ret;
   }

But it gives me error like this "id is null or not an object".

1
  • are you trying to combine two arrays? why you doing this x[i]<y[j]? Commented Mar 20, 2013 at 7:57

1 Answer 1

0
var temp = 0;

for(var i=0;i<count_array.length;i++)
{
for(var j=0;j<temp_array.length;i++)
{
if(count_array[i].id == temp_array[j].id)
{
temp_array_cnt[temp].left= count_array[i].left;
temp_array_cnt[temp].width= count_array[i].width;
temp_array_cnt[temp].id= count_array[i].id;
temp++;
}
}
}
0

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.