📌  相关文章
📜  比较 javascript 中的两个数组返回差异 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:58.839000             🧑  作者: Mango

代码示例4
function arrayDiff (a1, a2) {
    var a = [], diff = [];
    for (var i = 0; i < a1.length; i++) {
        a[a1[i]] = true;
    }
    for (var i = 0; i < a2.length; i++) {
        if (a[a2[i]]) {
            delete a[a2[i]];
        } else {
            a[a2[i]] = true;
        }
    }
    for (var k in a) {
        diff.push(k);
    }
    return diff;
}
//usage:
console.log(arrayDiff(['red', 'white','green'], [ 'red','white', 'blue']));//["green", "blue"]