拖动项目后,我的更新位置出现问题。所以我有数组对象:
[
{id: 1, name: "AAA", position:1},
{id: 2, name: "BBB", position:2},
{id: 3, name: "CCC", position:3},
{id: 4, name: "DDD", position:4}
]
然后我想将第3项移到顶部变为:
[
{id: 3, name: "CCC", position:3},
{id: 1, name: "AAA", position:1},
{id: 2, name: "BBB", position:2},
{id: 4, name: "DDD", position:4}
]
那么如何更新这样的数组:
[
{id: 3, name: "CCC", position:1},
{id: 1, name: "AAA", position:2},
{id: 2, name: "BBB", position:3},
{id: 4, name: "DDD", position:4}
]
由于
答案 0 :(得分:0)
使用简单的forEach
条件。并使用数组更新位置值
增量
var ar = [
{id: 3, name: "CCC", position:3},
{id: 1, name: "AAA", position:1},
{id: 2, name: "BBB", position:2},
{id: 4, name: "DDD", position:4}
]
console.log(update_position(ar))
function update_position(arr){
arr.forEach(function(a,b){
a.position=b+1;
})
return arr
}
答案 1 :(得分:0)
var ar = [
{id: 3, name: "CCC", position:3},
{id: 1, name: "AAA", position:1},
{id: 2, name: "BBB", position:2},
{id: 4, name: "DDD", position:4}
]
console.log(update_position(ar))
function update_position(arr){
arr.forEach(function(a,b){
a.position=b+1;
})
return arr
}

由于