You need to enable JavaScript to run this app. Google Apps Script 数组比对:将array_main缺失元素补入array_helper_both
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script 数组比对:将array_main缺失元素补入array_helper_both

问题原因

JS中数组属于引用类型,Array.includes()执行的是严格相等(===)比对,两个内容完全一致的独立数组因为内存地址不同,会被判定为不相等,这是原有代码不生效的核心原因。

可行实现方案

思路是先把每个子数组序列化为可直接比对的基础类型(比如字符串),再做存在性校验,找到差异项后追加到目标数组即可。

代码实现

// 把array_helper_both的子元素转成字符串,存入Set方便快速查询
const helperSet = new Set(array_helper_both.map(item => JSON.stringify(item)))
// 筛选出array_main中不存在于helperSet的元素
const diffItems = array_main.filter(item => !helperSet.has(JSON.stringify(item)))
// 合并到array_helper_both末尾
array_helper_both.push(...diffItems)

如果子数组元素顺序固定,也可以用拼接字符串的方式替代JSON.stringify,性能更好:

const helperSet = new Set(array_helper_both.map(item => `${item[0]}-${item[1]}`))
const diffItems = array_main.filter(item => !helperSet.has(`${item[0]}-${item[1]}`))
array_helper_both.push(...diffItems)
验证结果

用示例数据运行后,array_helper_both的输出和预期结果完全一致:

[[01-0158969,315],[01-0161045,699],[01-0161050,500],[01-0161046,500]]

内容的提问来源于stack exchange,提问作者user3447653

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 10:36:04