2 回答

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以對(duì)訂單使用閉包,并在必要時(shí)進(jìn)行更新。
const data = [{id: 1, name: 'test1', order: 1}, {id: 2, name: 'qos1', isDeleted: true, order: 2}, {id: 3, name: 'qos2', order: 3}];
data.forEach((order => o => {
if (!o.isDeleted) o.order = order++;
})(1))
console.log(data);

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
創(chuàng)建一個(gè) external counter,并且僅在該項(xiàng)目未被刪除時(shí)才增加它:
const arr = [{id: 1, name: 'test1', order: 1}, {id: 2, name: 'qos1', isDeleted: true, order: 2}, {id: 3, name: 'qos2', order: 3}]
let counter = 1
arr.forEach(item => {
item.order = counter
counter += !item.isDeleted // casting boolean to number false - 0, true - 1
})
console.log(arr)
添加回答
舉報(bào)