3 回答
TA贡献1777条经验 获得超3个赞
您可以通过获取值的索引来映射对象。
const
sample = [{ key:"key1", title: "title1", value: "value1" }, { key:"key2", title: "title2", value: "value2" }, { key:"key3", title: "title3", value: "value3" }],
result = sample.map((o, i) => ({ ...o, title: o.title + (i + 1) }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
TA贡献1797条经验 获得超4个赞
希望这会有所帮助:) 根据需要在地图中转换值!:)
const sample = [
{
key:"key1",
title: "title1 + 1",
value: "value1"
},
{
key:"key2",
title: "title2 + 2",
value: "value2"
},
{
key:"key3",
title: "title3 + 3",
value: "value3"
}
];
const other = sample.map(val => ({...val, title: val.title + 'anything'}));
console.log(other);
TA贡献1871条经验 获得超13个赞
const sample = [
{
key:"key1",
title: "title1",
value: "value1"
},
{
key:"key2",
title: "title2",
value: "value2"
},
{
key:"key3",
title: "title3",
value: "value3"
}]
let result = sample.map((x, i) => {
x.title = x.title + ' + ' + (i + 1);
return x;
});
console.log(result);
添加回答
举报