Lodash-DifferenceBy具有不同的身份 [英] Lodash - DifferenceBy with different identity

查看:56
本文介绍了Lodash-DifferenceBy具有不同的身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

[
  {
    id: 1
  },
  {
    id: 2
  },
  {
    id: 3
  },
  {
    id: 4
  }
]

我的应用程序每隔5秒就会收到一个新数组,因此我需要比较下一个数组之间的差异...

Every 5 seconds my application receives a new array and I need to compare the difference between the next one...

所以下一个数组是:

[
  {
    conversation_id: 1
  },
  {
    conversation_id: 2
  },
  {
    conversation_id: 4
  }
]

考虑到身份是不同的.如何与上一个比较并获得包含排除项的数组?

Considering that identity is different. How can I compare with the previous and get an array with the excluded item?

[
  {
    id: 3
  }
]

推荐答案

使用_.differenceWith():

const prev = [{"id":1},{"id":2},{"id":3},{"id":4}]
const next = [{"conversation_id":1},{"conversation_id":2},{"conversation_id":4}]

const diff = _.differenceWith(prev, next, ({ id }, { conversation_id }) => _.eq(id, conversation_id))
console.log(diff)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>

这篇关于Lodash-DifferenceBy具有不同的身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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