1

I am trying to return an array of keys from a nested array based on two conditions using Ramda:

  1. object has a property delete: false
  2. object does not have property delete

So my array looks something like this:

  const array = [{id: 1, name: "test1"}, {id: 2, name: "test2", delete: true}, {id: 3, name: "test3", delete: false}]

So the result should be [1, 3]

So far I have this statement, which partially works, it returns the ids for the objects that don't have property delete, I am not sure how to check if property had delete: false and return it in the result:

map(
    pipe(
      ifElse(
        has("delete"),
        pipe(propEq("delete", false), prop("id")),
        prop("id")
      )
    ))(array)
0

2 Answers 2

3

Reject all items with delete: true, and then get an array of ids using R.pluck:

const { pipe, reject, prop, pluck } = R

const fn = pipe(
  reject(prop('delete')), // remove all items with delete: true
  pluck('id') // take the ids
)

const array = [{id: 1, name: "test1"}, {id: 2, name: "test2", delete: true}, {id: 3, name: "test3", delete: false}]

const result = fn(array)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

If you really want to be specific about the conditions, use R.propSatisfies with R.either, and use R.equals to specify the exact values. However, this is pretty redundant in your case:

const { pipe, filter, propSatisfies, either, equals, pluck } = R

const fn = pipe(
  filter(propSatisfies(
    either(equals(false), equals(undefined)),
    'delete'
  )), // remove all items with delete: true
  pluck('id') // take the ids
)

const array = [{id: 1, name: "test1"}, {id: 2, name: "test2", delete: true}, {id: 3, name: "test3", delete: false}]

const result = fn(array)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Sign up to request clarification or add additional context in comments.

3 Comments

What if I needed to get an id from group inside the object {id: 1, name: "test1", group: {id: 45}}. I tried path but it return undefined.
Use map(path(['group', 'id'])) instead of pluck.
Yep, just figured it out. Thank you for the help!
0

You can simply achieve this by checking for the property with the help of Object.hasOwn() and filtered out with the help of Array.filter() method.

Live Demo :

const array = [{id: 1, name: "test1"}, {id: 2, name: "test2", delete: true}, {id: 3, name: "test3", delete: false}];

const res = array.filter(obj => !Object.hasOwn(obj, 'delete') || !obj.delete).map(({ id }) => id);

console.log(res);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.