比较 google 应用程序脚本中的列表
Compared lists in google apps script
我正在尝试比较 2 个列表并查找 list1 中不存在于 list2 中的元素。
list1: [01-0161051, 01-0161053, 01-0161057, 01-0161058, 01-0161065, 01-0161067, 01-0161068]
list2: [01-0161051, 01-0161053, 01-0161057, 01-0161058, 01-0161065, 01-0161066]
list1 中但不在 list2 中的元素是 [01-0161067, 01-0161068]
我试过这段代码,但没有产生预期的结果:
missing = null,
i = list1.length;
while(i) {
missing = ( ~list1.indexOf(list2[--i]) ) ? missing : list1[i];
}
如有线索,我们将不胜感激。
function findInOneNotInTwo() {
const list1 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161067', '01-0161068'];
const list2 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161066'];
let in1notin2 = [];
list1.forEach(s=>{
if(!~list2.indexOf(s)) {
in1notin2.push(s);
}
});
Logger.log(in1notin2.join(', '));
}
Execution log
1:54:09 PM Notice Execution started
1:54:11 PM Info 01-0161067, 01-0161068
1:54:10 PM Notice Execution completed
var list1 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161067', '01-0161068'];
var list2 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161066'];
var missing = list1.filter(x => !list2.includes(x));
console.log(missing); // [ '01-0161067', '01-0161068' ]
我正在尝试比较 2 个列表并查找 list1 中不存在于 list2 中的元素。
list1: [01-0161051, 01-0161053, 01-0161057, 01-0161058, 01-0161065, 01-0161067, 01-0161068]
list2: [01-0161051, 01-0161053, 01-0161057, 01-0161058, 01-0161065, 01-0161066]
list1 中但不在 list2 中的元素是 [01-0161067, 01-0161068]
我试过这段代码,但没有产生预期的结果:
missing = null,
i = list1.length;
while(i) {
missing = ( ~list1.indexOf(list2[--i]) ) ? missing : list1[i];
}
如有线索,我们将不胜感激。
function findInOneNotInTwo() {
const list1 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161067', '01-0161068'];
const list2 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161066'];
let in1notin2 = [];
list1.forEach(s=>{
if(!~list2.indexOf(s)) {
in1notin2.push(s);
}
});
Logger.log(in1notin2.join(', '));
}
Execution log
1:54:09 PM Notice Execution started
1:54:11 PM Info 01-0161067, 01-0161068
1:54:10 PM Notice Execution completed
var list1 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161067', '01-0161068'];
var list2 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161066'];
var missing = list1.filter(x => !list2.includes(x));
console.log(missing); // [ '01-0161067', '01-0161068' ]