藉助includes()方法求兩個數組的差集(適用於對象數組)

var a = [1, 2, {s:3}, {s:4}, {s:5}].map(item => JSON.stringify(item))
var b = [{s:2}, {s:3}, {s:4}, 'a'].map(item => JSON.stringify(item))

var diff = a.concat(b)
            .filter(v => !a.includes(v) || !b.includes(v))
            .map(item => JSON.parse(item))
            
// diff: [1, 2, {s:5}, {s:2}, "a"]

原文連接:http://www.javashuo.com/article/p-ehpqcfhc-cc.htmlpost

相關文章
相關標籤/搜索