将对象从数组A移动到数组B

问题描述 投票:0回答:1

通过move在数组中移动项目非常简单,但不幸的是,不适用于我这种情况。

例如,我需要将索引为0的对象从#31组移动到#33,并将目标数组中对象的新索引设置为1。

  1. source_group_id = 31
  2. source_object_index = 0
  3. destination_group_id = 33
  4. destination_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"
    }
  ]
}

有人知道如何解决这个问题吗?

javascript object functional-programming ramda.js
1个回答
0
投票

在普通的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);
© www.soinside.com 2019 - 2024. All rights reserved.