In my scenario, I have two arrays in JavaScript as shown 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;
The objective here is to extract an array with unique id values from the first two arrays, resulting in the following:
Third array:
temp_array_cnt[0].left= 0;
temp_array_cnt[0].width= 33;
temp_array_cnt[0].id= 1;
I attempted to achieve this using the code snippet below, however it did not work as expected:
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;
}
Your assistance in resolving this issue would be greatly appreciated.
Currently, I am utilizing the function provided below:
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;
}
Unfortunately, this implementation is throwing an error stating "id is null or not an object".