Move object from array A to array B. Ramda.js

Arthur

It's pretty simple to move item in array via move, but unfortunately it's not suitable in my case as usual.

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.

  • source_group_id = 31
  • source_object_index = 0
  • destination_group_id = 33
  • 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?

Ori Drori

You can use lenses to change the sub-objects, but you'll first need to get the item.

Start by using R.view with R.lensPath to get the item. Then use R.over with R.lenseProp to remove the item from sub-object at the source key and index (sk, si), and then to insert it to the target key and index (tk, ti).

Update: to add a target, if the key (tk) doesn't exist, use R.unless to check with R.has for the existence of tk, and if it doesn't add an empty array with R.assoc.

const { curry, view, lensPath, pipe, over, lensProp, remove, unless, has, assoc, insert } = R;

const fn = curry(({ key: sk, idx: si }, { key: tk, idx: ti }, obj) => {
  const item = view(lensPath([sk, si]), obj); // get the item
  
  return pipe(
    over(lensProp(sk), remove(si, 1)), // remove the item from the source
    unless(has(tk), assoc(tk, [])), // add target if it's missing
    over(lensProp(tk), insert(ti, item)), // move to the target
  )(obj);
});

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" }] };

console.log(fn({ key: '31', idx: 0 }, { key: 33, idx: 1 }, stuff));
console.log(fn({ key: '31', idx: 0 }, { key: 555, idx: 1 }, stuff));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete object by id from array Ramda

From Dev

Ramda - Pick from an object and return an array

From Dev

How to move value from JSON Object to JS array

From Dev

Convert array of objects to one Object using ramda.js

From Dev

Find an object by id in array Ramda

From Dev

Insert a new object to array Ramda

From Dev

change an object property in array with ramda

From Dev

Ramda sort array of nested object

From Javascript

Ramda - Convert array of objects into object

From Dev

How to use Ramda remove to remove empty object from Array of objects?

From Dev

Ramda.js - how to view many values from a nested array

From Dev

Move object values from objec to array in RethinkDB

From Dev

Move object from one array to another

From Dev

Remove array from array of arrays using Ramda?

From Dev

Mongodb how to move specific object from array to another array?

From Dev

Move specified JSON object from array to a different array

From Dev

JS: Removing Object from json array with array

From Dev

Move object in array to end

From Dev

JS: Clicking on a object from array

From Dev

JS - deleting object from array

From Javascript

Ramda: convert key value array to object

From Dev

Pointfree Join Array to String, by key in object, in Ramda

From Dev

How to transform object map to array with Ramda?

From Dev

Is value in array or string inside object Ramda

From Dev

Convert array of arrays to object of arrays, using ramda

From Dev

Ramda: Duplicate an object by its (array) property

From Dev

How to convert an array of objects in an array of integers extracting values from those objects using ramda.js?

From Dev

Ramda - Get one array from multiple objects

From Dev

Ramda - how to get multiple properties from array

Related Related

  1. 1

    Delete object by id from array Ramda

  2. 2

    Ramda - Pick from an object and return an array

  3. 3

    How to move value from JSON Object to JS array

  4. 4

    Convert array of objects to one Object using ramda.js

  5. 5

    Find an object by id in array Ramda

  6. 6

    Insert a new object to array Ramda

  7. 7

    change an object property in array with ramda

  8. 8

    Ramda sort array of nested object

  9. 9

    Ramda - Convert array of objects into object

  10. 10

    How to use Ramda remove to remove empty object from Array of objects?

  11. 11

    Ramda.js - how to view many values from a nested array

  12. 12

    Move object values from objec to array in RethinkDB

  13. 13

    Move object from one array to another

  14. 14

    Remove array from array of arrays using Ramda?

  15. 15

    Mongodb how to move specific object from array to another array?

  16. 16

    Move specified JSON object from array to a different array

  17. 17

    JS: Removing Object from json array with array

  18. 18

    Move object in array to end

  19. 19

    JS: Clicking on a object from array

  20. 20

    JS - deleting object from array

  21. 21

    Ramda: convert key value array to object

  22. 22

    Pointfree Join Array to String, by key in object, in Ramda

  23. 23

    How to transform object map to array with Ramda?

  24. 24

    Is value in array or string inside object Ramda

  25. 25

    Convert array of arrays to object of arrays, using ramda

  26. 26

    Ramda: Duplicate an object by its (array) property

  27. 27

    How to convert an array of objects in an array of integers extracting values from those objects using ramda.js?

  28. 28

    Ramda - Get one array from multiple objects

  29. 29

    Ramda - how to get multiple properties from array

HotTag

Archive