我有以下数组:

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


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

所以下一个数组是:

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


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

[
  {
    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>

关于javascript - Lodash-区别通过不同的身份,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54219162/

10-13 04:10