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

如何在JavaScript中为JSON数组的title字段添加序号进行转换

给JSON数组的title字段添加序号

你可以用JavaScript的数组map方法批量处理,遍历数组时给每个对象的title字段加上对应序号:

// 原始数组
const originalArr = [
  {title:'button'},
  {title:'button'},
  {title:'button'},
  {title:'button'},
];

// 生成带序号的新数组
const newArr = originalArr.map((item, index) => {
  return {
    title: `${item.title} ${index + 1}`
  };
});

执行后得到的结果就是目标格式:

[
  {title:'button 1'},
  {title:'button 2'},
  {title:'button 3'},
  {title:'button 4'},
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 16:27:24