亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

lodash 兩個對象數組的差異

lodash 兩個對象數組的差異

萬千封印 2023-08-24 15:57:46
useEffect我的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)lodashis的邏輯_.xor是返回兩個數組之間的差異,但是,事實并非如此我得到的回報如下0: Object {id: 1, nome: "Ruan"}1: Object {id: 2, nome: "Gleison"}2: Object {id: 2, nome: "Gleison"}3: Object {id: 3, nome: "Geraldo"}我感謝所有提供幫助的努力
查看完整描述

2 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

您可以使用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)

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>


查看完整回答
反對 回復 2023-08-24
?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

這是因為即使它們的內容相同,對象也不相等,因為它們在內存中的地址不同。

你可以嘗試這樣的事情:

const results = _.xor(A1.map(object=> JSON.stringify(object)), A2.map(object=> JSON.stringify(object))).map(item => JSON.parse(item));


查看完整回答
反對 回復 2023-08-24
  • 2 回答
  • 0 關注
  • 287 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號