You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apps Script如何从数组中选取可变索引指定的对应列

问题解决方法

错误原因

你封装的函数没有返回map方法的执行结果,所以调用后默认返回空值,和你看到的null表现一致。

修正后的代码

function newArrayFunction(n) {
  // 新增return把map生成的新数组返回出去
  return data.map(function(column) { 
    return column[n];
  });
}

如果你用的环境支持ES6+语法,也可以用箭头函数简化写法:

const newArrayFunction = n => data.map(column => column[n]);

验证效果

调用newArrayFunction(3)就能得到预期的["Submarine", "Boats", "duh", "cars"]输出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 00:21:03