如何在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
相关产品推荐
相关产品推荐

