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

Ricardo Montuan

I'm trying to convert an array of objects into an array of integers extracting values from those objects using Ramda.js. I need to keep just the node participants with the uid values, however, it seems that I'm not doing this correctly.

I'm want to transform this

var listObejcts = {
  "participants": [
    {
      "entity": {
        "uid": 1
      }
    },
    {
      "entity": {
        "uid": 2
      }
    }
  ]
}

to this:

{
  "participants": [1, 2]
}

I've tried the code above but it didn't work. It's still returning a list of objects.

var transform = pipe(
  over(lensProp('participants'), pipe(
    filter(pipe(
      over(lensProp('entity'), prop('uid'))
    ))
  ))
)

console.log(transform(listObejcts))

Does anybody know how I could achieve that?

It's possible to edit the code here - https://repl.it/repls/PrimaryMushyBlogs

Scott Sauyet

One possibility is to combine evolve with map(path) like this:

const transform = evolve({participants: map(path(['entity', 'uid']))})

var listObjects = {participants: [{entity: {uid: 1}}, {entity: {uid: 2}}]}

console.log(transform(listObjects))
<script src="https://bundle.run/[email protected]"></script><script>
const {evolve, map, path} = ramda  </script>

While I'm sure that there is a lens-based solution, this version looks pretty straightforward.

Update

A lens-based solution is certainly possible. Here is one such:

var transform = over(
  lensProp('participants'), 
  map(view(lensPath(['entity', 'uid'])))
)

var listObjects = {participants: [{entity: {uid: 1}}, {entity: {uid: 2}}]}

console.log(transform(listObjects))
<script src="https://bundle.run/[email protected]"></script><script>
const {over, lensProp, map, view, lensPath} = ramda  </script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert an array of objects into an array of arrays using Ramda

Convert array of objects to one Object using ramda.js

How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values?

Ramda - Convert array of objects into object

Extracting values from Array of nested Objects in JavaScript

Problem extracting values from an array of objects in TypeScript

Convert Array of objects to array of integers

JS: Extracting data from an array of objects

Transform array of objects to nested objects using Ramda

Convert array of objects into an array of values from that object

How to map desired data from an array of objects and return two new objects containing those values

Extracting values from array nested inside array of objects in javscript

convert array to array of objects using JS

Match values of a key in a nested array of objects to an identifier key using ramda.js

Reduce Array of Array of Objects Using Ramda

extracting info from array objects

How to transform array of arrays into an array of objects with Ramda?

How do I convert an array of integers to an array of objects?

ramda.js: obtain a set of duplicates from an array of objects using specific properties

How to count occurrences of a path in an array of objects with ramda.js?

Convert an array of objects to array of the objects' values

Extracting data from an array of JSON objects for specific object values

Extracting all values from a Array of Objects based on keys value

How to map an array of objects to a single object using Ramda

How to sort an array of objects based on nested key using Ramda?

How to copy an array of object and change only objects indexes using Ramda?

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

Create array from values of different objects using Underscore.js

Getting array of school values from all objects using JS