带破折号的两个对象数组的区别 [英] Difference of two arrays of objects with lodash

查看:36
本文介绍了带破折号的两个对象数组的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在reactJS的 useEffect 中有以下代码

I have the following code inside my useEffect in reactJS

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

const results = _.xor(A1, A2);

console.log(results)

lodash 的逻辑是 _.xor 是要返回两个数组之间的差,但是,这不是正在发生的事情

The logic of lodash is _.xor is to return the difference between the two arrays, however, that is not what is happening

我得到的回报如下

0: Object {id: 1, nome: "Ruan"}
1: Object {id: 2, nome: "Gleison"}
2: Object {id: 2, nome: "Gleison"}
3: Object {id: 3, nome: "Geraldo"}

我非常感谢您提供的所有帮助

I appreciate all efforts to help

推荐答案

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

You can use xorBy to indicate a property used for comparison:

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)

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>

这篇关于带破折号的两个对象数组的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆