vue数据处理的问题

vue数据处理的问题

如何根据数组长度对数组对象内的某个属性按照顺序赋值
比如把现在的数组的number按照顺序赋值分别变成0,1,2,3,只更改number的值,应该如何处理,感谢各位大佬

let arr = [

{

"id": "数据1",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 0

},

{

"id": "数据2",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 1

},

{

"id": "数据3",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 4

},

{

"id": "数据4",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 3

}

]

想要改成这样:
[

{

"id": "数据1",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 0

},

{

"id": "数据2",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 1

},

{

"id": "数据3",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 2

},

{

"id": "数据4",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 3

}


回答:

楼主看下是不是这个操作

arr.forEach((item, index) =>{

item.number = index

})


回答:

arr.map((item, idx) => ({ ...item, number: idx })) ,是这个意思么?


回答:

我理解是将数组数据更具数组中的number排序,如下:
let arr = [

{

"id": "数据1",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 0

},

{

"id": "数据2",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 1

},

{

"id": "数据3",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 4

},

{

"id": "数据4",

"parentId": "91453e7d-11bc-443e-a2eb-c1695c4aed3f",

"number": 3

}

]

// 核心是这里

arr.sort((a,b)=>{

return a.number - b.number

})

补充下:如果要倒叙就 b.number - a.number

以上是 vue数据处理的问题 的全部内容, 来源链接: utcz.com/p/936421.html

回到顶部