lodash 两个对象数组的差异

Rua*_*rte 2 javascript lodash react-hooks

useEffect我的reactJS中有以下代码

\n
const A1 = [{id: 1, nome: "Ruan"}, {id: 2, nome: "Gleison"}]\nconst A2 = [{id: 2, nome: "Gleison"}, {id: 3, nome: "Geraldo"}]\n\nconst results = _.xor(A1,\xc2\xa0A2);\n\nconsole.log(results)\n
Run Code Online (Sandbox Code Playgroud)\n

lodashis的逻辑_.xor是返回两个数组之间的差异,但是,事实并非如此

\n

我得到的回报如下

\n
0: Object {id: 1, nome: "Ruan"}\n1: Object {id: 2, nome: "Gleison"}\n2: Object {id: 2, nome: "Gleison"}\n3: Object {id: 3, nome: "Geraldo"}\n
Run Code Online (Sandbox Code Playgroud)\n

我感谢所有提供帮助的努力

\n

Dig*_*ter 5

您可以使用xorBy来指示用于比较的属性:

const A1 = [{id: 1, nome: "Ruan"}, {id: 2, nome: "Gleison"}]
const A2 = [{id: 2, nome: "Gleison"}, {id: 3, nome: "Geraldo"}]

const results = _.xorBy(A1, A2, 'id'); // or 'nome'

console.log(results)
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
Run Code Online (Sandbox Code Playgroud)