0
点赞
收藏
分享

微信扫一扫

js批量给数组元素添加序号

_karen 2023-04-22 阅读 67


let arr = [
    {text:"测试1"},
    {text:"测试2"},
 ]

降序

arr.forEach((item,index)=>{
     item.index=arr.length-index;
 });
 console.log(arr);
 
 输出:
 [
    {
        "text": "测试1",
        "index": 2
    },
    {
        "text": "测试2",
        "index": 1
    }
]

增序

arr.forEach((item,index)=>{
     item.index=index +1;
 });
 console.log(arr);


 输出:
 [
    {
        "text": "测试1",
        "index": 1
    },
    {
        "text": "测试2",
        "index": 2
    }
]

在列表数据变化处调动


举报

相关推荐

0 条评论