将一维数组转换为二维数组并将数组附加到 Sheet 的最后一行

Convert 1D Array to 2D Array and append Array to last Row of Sheet

我有 2 个数组列表,我从中发现了差异。

arr1 = ['Sam','Annie', 'Margorie', 'Ester', 'John'];
arr2 = ['Bill', 'Sally', 'Sam', 'Annie', 'Suman', 'Margorie', 'Ester', 'Pierre', 'John'];

var diff = arr2.filter(x => !arr1.includes(x));

console.log(diff) //result....diff = ['Bill', 'Sally', 'Suman', 'Pierre']

我想做的是修改上面的Filter Arrow函数,为每一项添加'New',这样得到的数组就变成:

//diff = [['Bill', 'New'] , ['Sally', 'New'] , ['Suman', 'New'] , ['Pierre' , 'New']]

我可以像这样使用地图箭头函数单独执行此操作:

diff = diff.map(x => [x,'New']);

例如

Sht.appendRow(diff);

我做错了什么?

要合并 map/filter,请使用简单的 for-loop/if 或减少:

arr1 = ['Sam','Annie', 'Margorie', 'Ester', 'John'];
arr2 = ['Bill', 'Sally', 'Sam', 'Annie', 'Suman', 'Margorie', 'Ester', 'Pierre', 'John'];

const diff = arr2.reduce((diff,x) => !arr1.includes(x) ? [...diff , [x, "New"]] : diff, [] );

console.log(diff) //result....diff = ['Bill', 'Sally', 'Suman', 'Pierre']

Sheet#appendRow 只接受 1D(一维)数组。如果要附加二维数组,请使用 range.setValues()