比较 2 个列表并找出 google 应用程序脚本中的差异
Compare 2 lists and spot differences in google apps script
我正在尝试比较 2 个列表并获取它们之间的值差异
list1 = [01-0161045, 01-0161046, 01-0158587, 01-0158647, 01-0158262, 01-0158264]
list2 = [01-0161045, 01-0161046, 01-0158587, 01-0158647, 01-0158262, 01-0158263, 01-0158265]
在上面的2个列表中,首先我需要比较list2和list1,区别是“01-0158263”和“01-0158265”。这个差异需要吐槽
一个不同的列表(比如 list3)
list3 = [01-0158263, 01-0158265]
接下来,我将 list1 与 list2 进行比较,差异是“01-0158264”。该值需要附加到 list2 的末尾。我们可以将结果保存到list4.
list4 = [01-0161045, 01-0161046, 01-0158587, 01-0158647, 01-0158262, 01-0158263, 01-0158265, 01-0158264].
我已经尝试在 Google Apps 脚本中使用 2 个 for 循环来完成它,但是当列表增长时需要很长时间。只是想知道有没有一种方法可以使用列表比较来有效地做到这一点。
感谢您的帮助!!
为了达到你的目的,下面的示例脚本怎么样?在此示例脚本中,从 list2
和 list3
创建了 2 个对象。并且,使用这些对象,创建了 list3
和 list4
。
示例脚本:
const list1 = ["01-0161045", "01-0161046", "01-0158587", "01-0158647", "01-0158262", "01-0158264"];
const list2 = ["01-0161045", "01-0161046", "01-0158587", "01-0158647", "01-0158262", "01-0158263", "01-0158265"];
const obj1 = list1.reduce((o, e) => Object.assign(o, {[e]: true}), {});
const obj2 = list2.reduce((o, e) => Object.assign(o, {[e]: true}), {});
const list3 = list2.filter(e => !obj1[e]);
const list4 = list2.concat(list1.filter(e => !obj2[e]));
console.log(list3)
console.log(list4)
参考文献:
为什么不将 filter()
与 includes()
一起使用?
var list1 = ["01-0161045", "01-0161046", "01-0158587", "01-0158647", "01-0158262", "01-0158264"]
var list2 = ["01-0161045", "01-0161046", "01-0158587", "01-0158647", "01-0158262", "01-0158263", "01-0158265"]
var list3 = list2.filter(x => !list1.includes(x));
var list4 = list2.concat(list1.filter(x => !list2.includes(x)));
console.log(list3);
console.log(list4);
虽然我不知道它在性能方面是否更好。
我正在尝试比较 2 个列表并获取它们之间的值差异
list1 = [01-0161045, 01-0161046, 01-0158587, 01-0158647, 01-0158262, 01-0158264]
list2 = [01-0161045, 01-0161046, 01-0158587, 01-0158647, 01-0158262, 01-0158263, 01-0158265]
在上面的2个列表中,首先我需要比较list2和list1,区别是“01-0158263”和“01-0158265”。这个差异需要吐槽 一个不同的列表(比如 list3)
list3 = [01-0158263, 01-0158265]
接下来,我将 list1 与 list2 进行比较,差异是“01-0158264”。该值需要附加到 list2 的末尾。我们可以将结果保存到list4.
list4 = [01-0161045, 01-0161046, 01-0158587, 01-0158647, 01-0158262, 01-0158263, 01-0158265, 01-0158264].
我已经尝试在 Google Apps 脚本中使用 2 个 for 循环来完成它,但是当列表增长时需要很长时间。只是想知道有没有一种方法可以使用列表比较来有效地做到这一点。
感谢您的帮助!!
为了达到你的目的,下面的示例脚本怎么样?在此示例脚本中,从 list2
和 list3
创建了 2 个对象。并且,使用这些对象,创建了 list3
和 list4
。
示例脚本:
const list1 = ["01-0161045", "01-0161046", "01-0158587", "01-0158647", "01-0158262", "01-0158264"];
const list2 = ["01-0161045", "01-0161046", "01-0158587", "01-0158647", "01-0158262", "01-0158263", "01-0158265"];
const obj1 = list1.reduce((o, e) => Object.assign(o, {[e]: true}), {});
const obj2 = list2.reduce((o, e) => Object.assign(o, {[e]: true}), {});
const list3 = list2.filter(e => !obj1[e]);
const list4 = list2.concat(list1.filter(e => !obj2[e]));
console.log(list3)
console.log(list4)
参考文献:
为什么不将 filter()
与 includes()
一起使用?
var list1 = ["01-0161045", "01-0161046", "01-0158587", "01-0158647", "01-0158262", "01-0158264"]
var list2 = ["01-0161045", "01-0161046", "01-0158587", "01-0158647", "01-0158262", "01-0158263", "01-0158265"]
var list3 = list2.filter(x => !list1.includes(x));
var list4 = list2.concat(list1.filter(x => !list2.includes(x)));
console.log(list3);
console.log(list4);
虽然我不知道它在性能方面是否更好。