繁体   English   中英

从两个数组中获取javascript中第三个数组的唯一值

[英]From two array get unique value in third array in javascript

我有两个如下的javascript数组对象

第一个数组:

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;

第二个数组:

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;

我想从像下面这样的两个数组中获取javascript中id唯一值的数组

第三数组:

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

我正在使用以下代码,但无法正常工作

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;

}

请在这里帮助我。 谢谢

我现在正在使用以下功能

   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;
   }

但是它给我这样的错误,例如“ id为null或不是对象”。

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++;
}
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM