从第一个数组Jquery中存在的第二个数组中删除所有内容

时间:2016-04-01 13:14:47

标签: jquery

我的Jquery中有两个数组

     var results = ["1","2","3","4","5","6"];
     var chosen = ["1","A","3","B","5","C"];

如何删除所选结果中的所有内容?

3 个答案:

答案 0 :(得分:1)

 for(var i = 0; i < chosen.length; i++){
     if(results.indexOf(chosen[i]) > -1){
          results.splice(results.indexOf(chosen[i]),1);
      }
}
console.log(results);

results =&gt; 2,4,6-

答案 1 :(得分:0)

对结果做一个foreach并检查值是否包含在所选数组中,如果是,则删除值

答案 2 :(得分:0)

尝试,

var results = ["1","2","3","4","5","6"];
var chosen = ["1","A","3","B","5","C"];

var cnt = chosen.length;
while(--cnt, cnt >= 0) {
 if(results.indexOf(chosen[cnt]) > -1){
   chosen.splice(cnt,1)
 }
}
  

从第一个数组Jquery

中存在的第二个数组中删除所有内容

因此console.log(chosen); // "A","B","C"将是结果。