<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>在一个数组中删除另一个数组存在的值</title>
<style type="text/css">
html,body{100%;height:100%;margin:0;}
</style>
</head>
<body>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script type="text/javascript">
//方法一:
var arr = [1, 2, 3, 4, '1', 0, 1, 2, 'a', 'b'];
var brr = [1, 2, 'a'];
for(var i = 0;i < brr.length; i++) {
for(var j = 0; j < arr.length; j++) {
if(arr[j] === brr[i]) {
arr.splice(j,1);
j--;
}
}
}
console.log(arr);
//方法二(高效):
var arr = [1, 2, 3, 4, '1', 0, 1, 2, 'a', 'b'];
var brr = [1, 2, 'a'];
var temp = [];
var temparray = [];
for (var i = 0; i < brr.length; i++) {
temp[brr[i]] = typeof brr[i];;
}
for (var i = 0; i < arr.length; i++) {
var type = typeof arr[i];
if (!temp[arr[i]]) {
temparray.push(arr[i]);
} else if (temp[arr[i]].indexOf(type) < 0) {
temparray.push(arr[i]);
}
}
console.log(temparray);
</script>
</body>
</html>