比較數組,獲得數組不一樣的部分。git
var diff = require('arr-diff'); var a = ['a', 'b', 'c', 'd']; var b = ['b', 'c']; console.log(diff(a, b)) //=> ['a', 'd']
module.exports = function diff(arr/*, arrays*/) { var len = arguments.length; var idx = 0; // 屢次調用 比較方法比較多個數組 while (++idx < len) { arr = diffArray(arr, arguments[idx]); } return arr; }; function diffArray(one, two) { if (!Array.isArray(two)) { return one.slice(); } var tlen = two.length var olen = one.length; var idx = -1; var arr = []; // 取 one 中的元素和 two 中的元素對比 while (++idx < olen) { var ele = one[idx]; var hasEle = false; for (var i = 0; i < tlen; i++) { var val = two[i]; if (ele === val) { hasEle = true; break; } } if (hasEle === false) { arr.push(ele); } } return arr; }