js 实现两个数组（一个数组包含于另一个数组中）元素组成第三个数组

b = [1,3]

2个回答

function getC(a,b){
Array.prototype.contains = function(item){
return RegExp(item).test(this);
};
r=new Array();
len= a.length;
for(var i=0;i<len;i++){
if(b.contains(a[i])){
r[i]=1;
}else{
r[i]=0;
}
}

return r;
}

var a = [1,2,3,4,5];
var b = [1,3];
var c = getC(a,b);

yedanrushui function getC(a,b){ Array.prototype.contains = function(item){ return RegExp(item).test(this); }; r=new Array(); len= a.length; for(var i=0;i<len;i++){ for(var j=0;j<a[i].length;j++){ if(b.contains(a[i][0])){ r[i]=[1,a[i][1],a[i][2]]; }else{ r[i]=[0,a[i][1],a[i][2]]; } } } return r; } var a = [[1,"11","aa"],[2,"12","bb"],[3,"13","cc"],[4,"14","dd"]]; var b = [1,3]; var c = getC(a,b); for(var i=0;i<c.length;i++){ for(var j=0;j<c[i].length;j++){ alert(c[i][j]); } }

yedanrushui 这样好像取巧了，a数据元素和i正好相同，如果a=[1,3,5,6,7],这样就不行了

tianchao_ function getC(a,b){ Array.prototype.contains = function(item){ return RegExp(item).test(this); }; r=new Array(); len= a.length; for(var i=0;i<len;i++){ if(b.contains(a[i])){ r[i]=[1,i]; }else{ r[i]=[0,i]; } } return r; } var a = [1,2,3,4,5]; var b = [1,3]; var c = getC(a,b); console.log(c);

yedanrushui 你好，我想问下，如果a= [1,2,3,4,5] ， b = [1,3] ，要c=[[1,1],[0,2],[1,3],[0,4],[0,5]] 形成一个二维数组结果有什么方法吗，谢谢

x= [1,2,3,4,5,6] , y = [1,3]
Array.prototype.mark = function(t)
{
var a = this, o = [];
for(var i in a)
{
o[i] = 0;
for(var j in t)
{
if (t[j] === a[i])
{
o[i]=1;
}
}
}
return o;
};
x.mark(y);