/**
* 数组相减的方法 - es5
* @param {Array} a
* @param {Array} b
*/
function arrSubtraction1(a, b) {
if (!a || !b || Object.prototype.toString.call(a) !== '[object Array]' || Object.prototype.toString.call(b) !== '[object Array]') {
throw new Error('arrSubtraction(): Wrong Param Type')
}
for (var i = 0, len = b.length; i < len; i++) {
for (var j = 0, jen = a.length; j < jen; j++) {
if (a[j] === b[i]) {
a.splice(j, 1);
j--
}
}
}
return a
} /**
* 数组相减的方法 - 使用es新特性
* @param {Array} a
* @param {Array} b
*/
function arrSubtraction2(a, b) {
if (Array.isArray(a) && Array.isArray(b)) {
return a.filter(i => !b.includes(i))
}
throw new Error('arrSubtraction(): Wrong Param Type')
}
05-07 15:11