如何获取两个对象数组之间的变化? [ lodash/ JS]
How to get changes between 2 arrays of objects? [ lodash/ JS]
我有一个 table 已经填充了来自 API 的数据集。对于演示,假设我已经在从 API 获取的 table 上添加了 2 行。现在,我编辑了第二行还添加了另一行。所以我想要实现的是我想要获得一个新的对象数组,其中包含我刚刚编辑的行以及我刚刚添加的行。
以下是我拥有的带有虚拟数据集的 2 个数组。
第一个数组:
[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 8, end: 36 }, point: 49 },
]
第二个数组:
[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 36, end: 97 }, point: 15 },
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
]
请注意,我编辑了 Second Array
中的第二行值。第二个数组中的第三行没有用户 ID,因为 userId
将在发布后从服务器返回
预期输出:
[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 36, end: 97 }, point: 15 },
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
]
我尝试了 lodash
_.differencWith
/ _.intersectWith
但使用它的输出如下所示
[
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
]
只返回 table 上新添加的行,但忽略了我也更改了第二行的值。
注意:table 只有 3 列,其中所有列都是 editable。 coun: { start, end}
和 point
您可以使用 _.differenceWith
(https://lodash.com/docs/4.17.15#differenceWith)
const first=[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 8, end: 36 }, point: 49 },
]
const second=[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 36, end: 97 }, point: 15 },
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
]
let result=_.differenceWith(second, first, _.isEqual)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
_.differenceWith
:
此方法与 _.difference
类似,不同之处在于它接受调用比较器来将数组元素与值进行比较。结果值的顺序和引用由第一个数组确定。使用两个参数调用比较器:(arrVal, othVal)
.
const a = [
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 8, end: 36 }, point: 49 },
]
const b = [
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 36, end: 97 }, point: 15 },
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
];
result = _.differenceWith(b, a, _.isEqual)
console.log(result); // your excpected result.
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
我有一个 table 已经填充了来自 API 的数据集。对于演示,假设我已经在从 API 获取的 table 上添加了 2 行。现在,我编辑了第二行还添加了另一行。所以我想要实现的是我想要获得一个新的对象数组,其中包含我刚刚编辑的行以及我刚刚添加的行。
以下是我拥有的带有虚拟数据集的 2 个数组。
第一个数组:
[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 8, end: 36 }, point: 49 },
]
第二个数组:
[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 36, end: 97 }, point: 15 },
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
]
请注意,我编辑了 Second Array
中的第二行值。第二个数组中的第三行没有用户 ID,因为 userId
将在发布后从服务器返回
预期输出:
[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 36, end: 97 }, point: 15 },
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
]
我尝试了 lodash
_.differencWith
/ _.intersectWith
但使用它的输出如下所示
[
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
]
只返回 table 上新添加的行,但忽略了我也更改了第二行的值。
注意:table 只有 3 列,其中所有列都是 editable。 coun: { start, end}
和 point
您可以使用 _.differenceWith
(https://lodash.com/docs/4.17.15#differenceWith)
const first=[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 8, end: 36 }, point: 49 },
]
const second=[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 36, end: 97 }, point: 15 },
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
]
let result=_.differenceWith(second, first, _.isEqual)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
_.differenceWith
:
此方法与 _.difference
类似,不同之处在于它接受调用比较器来将数组元素与值进行比较。结果值的顺序和引用由第一个数组确定。使用两个参数调用比较器:(arrVal, othVal)
.
const a = [
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 8, end: 36 }, point: 49 },
]
const b = [
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 36, end: 97 }, point: 15 },
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
];
result = _.differenceWith(b, a, _.isEqual)
console.log(result); // your excpected result.
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>