带破折号的两个对象数组的区别

编程入门 行业动态 更新时间:2024-10-24 07:32:14
本文介绍了带破折号的两个对象数组的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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="cdn.jsdelivr/npm/lodash@4.17.20/lodash.min.js"></script>

更多推荐

带破折号的两个对象数组的区别

本文发布于:2023-07-20 17:50:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169701.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:破折号   数组   区别   对象   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!