问题描述
比如a= [1,2,3,4,5,6] b = [1,3] 那么c应该为[1,0,1,0,0,0]或者a为这个也可以,这个该如何实现呢
解决方案
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);
解决方案二:
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);
时间: 2024-10-21 07:51:21