通过move在数组中移动项目非常简单,但不幸的是,不适用于我这种情况。
例如,我需要将索引为0的对象从#31组移动到#33,并将目标数组中对象的新索引设置为1。
source_group_id
= 31source_object_index
= 0destination_group_id
= 33destination_object_index
= 1数据对象模型:
const stuff = {
"31": [
{------------------------------|
"id": "11", |============|
"title": "just move me pls" | ||
},-----------------------------| ||
{ ||
"id": "12", ||
"title": "ramda 123" ||
}, ||
], ||
"33": [ ||
{ ||
"id": "3", ||
"title": "..." ||
} ||
◀==========================================|
],
"4321": [
{
"id": "1",
"title": "hello Ramda"
}
]
}
有人知道如何解决这个问题吗?
在普通的Java语言中,Array#splice
效果很好。
Array#splice
const stuff = { 31: [{ id: "11", title: "just move me pls" }, { id: "12", title: "ramda 123" }], 33: [{ id: "3", title: "..." }], 4321: [{ id: "1", title: "hello Ramda" }] };
stuff['33'].splice(1, 0, ...stuff['31'].splice(0, 1));
console.log(stuff);