Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

javascript - 将对象从数组A移到数组B。Ramda.js(Move object from array A to array B. Ramda.js)

It's pretty simple to move item in array via move , but unfortunately it's not suitable in my case as usual.(通过move在数组中移动项目非常简单,但是不幸的是,它不适用于我以往的情况。)

For example I need to move object with index 0 from group #31 to #33 and set the new index for object in the destination array to 1.(例如,我需要将索引为0的对象从组#31移动到#33,并将目标数组中对象的新索引设置为1。)

  • source_group_id = 31(source_group_id = 31)
  • source_object_index = 0(source_object_index = 0)
  • destination_group_id = 33(destination_group_id = 33)
  • destination_object_index = 1(destination_object_index = 1)

Data object model:(数据对象模型:)

const stuff = {
  "31": [
    {------------------------------|
      "id": "11",                  |============|
      "title": "Just move me pls"  |           ||
    },-----------------------------|           ||
    {                                          ||
      "id": "12",                              ||
      "title": "Ramda 123"                     ||
    },                                         ||
  ],                                           ||
  "33": [                                      ||
    {                                          ||
      "id": "3",                               ||
      "title": "Ramda jedi"                    ||
    }                                          ||
     ?==========================================|
  ],   
  "4321": [
    {
      "id": "1",
      "title": "Hello Ramda"
    }
  ]
}

Does anyone know how to solve this?(有谁知道如何解决这个问题?)

  ask by Arthur translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In plain Javascript, Array#splice works well.(在纯Javascript中, 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); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...