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

JavaScript中如何对二维数组仅拼接子数组的首个元素?

二维数组仅拼接子数组首个元素的实现方法

方法1:map + join 组合

先通过map()提取每个子数组的第一个元素,得到一维数组后再用join('')完成拼接:

const arr = [['abc',1],['def',2]];
const result = arr.map(item => item[0]).join('');
console.log(result); // 输出 'abcdef'

方法2:reduce 累加

利用reduce()逐个累加子数组的首个元素,初始值设为空字符串:

const arr = [['abc',1],['def',2]];
const result = arr.reduce((total, current) => total + current[0], '');
console.log(result); // 输出 'abcdef'

方法3:循环遍历拼接

用forEach或普通for循环遍历数组,手动拼接目标元素:

const arr = [['abc',1],['def',2]];
let result = '';
arr.forEach(item => {
  result += item[0];
});
console.log(result); // 输出 'abcdef'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 20:32:38