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

如何使用ES6提取嵌套数组子元素转字符串后推入对应父数组

实现方案

完全可以用ES6特性实现,代码如下:

const a1 = [['one', 'two', ['three', 'four']], ['five', 'six', ['seven', 'eight']]];
const a2 = a1.map(parentArr => 
  parentArr.map(item => Array.isArray(item) ? item.join(', ') : item)
);
console.log(a2);
// 输出结果和需求完全一致:[['one', 'two', 'three, four'], ['five', 'six', 'seven, eight']]

逻辑说明

  • 外层map遍历a1中的每一组父数组
  • 内层map遍历父数组的所有元素:如果元素是嵌套的子数组,调用join(', ')直接拼接为逗号后带空格的字符串;非数组类型的元素直接返回原值
  • 用到的所有语法(箭头函数、Array.prototype.map、Array.isArray)均为ES6标准支持的特性,无需额外兼容处理

内容的提问来源于stack exchange,提问作者R.H

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 09:36:04