samrumlin
samrumlin

Reputation: 1

How do I remove an object if it contains a key with an empty array?

I have an an array of objects. My objective is to remove objects that contain keys with empty arrays.

I am using ramda, but am hitting a wall at the moment.

const myData = {
  "one": {
    "two": {
      "id": "1",
      "three": [{
        "id": "33",
        "copy": [{
            "id": "1",
            "text": "lorem",
            "answer": [],
          },
          {
            "id": "2",
            "text": "ipsum",
            "answer": [{
              "id": 1,
              "class": "science"
            }]
          },
          {
            "id": "3",
            "text": "baesun",
            "answer": [{
              "id": 2,
              "class": "reading"
            }]
          }
        ],
      }]
    }

  }
}

flatten(pipe(
    path(['one', 'two', 'three']),
    map(step => step.copy.map(text => ({
      answers: text.answer.map(answer => ({
        class: answer.class,
      })),
    }), ), ))
  (myData))

This the result:

[{"answers": []}, {"answers": [{"class": "science"}]}, {"answers": [{"class": "reading"}]}]

This is the expectation:

[{"answers": [{"class": "science"}]}, {"answers": [{"class": "reading"}]}]

Upvotes: 0

Views: 367

Answers (2)

Ori Drori
Ori Drori

Reputation: 191986

Get the the array of inside three with path, chain the arrays inside the copy properties, and project them to contain only answer. Reject empty answers, and then evolve the objects inside each answer to contain only the class property.

const {pipe, path, chain, prop, project, reject, propSatisfies, isEmpty, map, evolve} = ramda

const transform = pipe(
  path(['one', 'two', 'three']), // get the array
  chain(prop('copy')), // concat the copy to a single array
  project(['answer']), // extract the answers 
  reject(propSatisfies(isEmpty, 'answer')), // remove empty answers
  map(evolve({ answer: project(['class']) })) // convert the objects inside each answer to contain only class
)

const data = {"one":{"two":{"id":"1","three":[{"id":"33","copy":[{"id":"1","text":"lorem","answer":[]},{"id":"2","text":"ipsum","answer":[{"id":1,"class":"science"}]},{"id":"3","text":"baesun","answer":[{"id":2,"class":"reading"}]}]}]}}}

const result = transform(data)

console.log(result)
<script src="//bundle.run/[email protected]"></script>

Upvotes: 3

ajai Jothi
ajai Jothi

Reputation: 2294

use filter

const filter = R.filter,
  flatten = R.flatten,
  pipe = R.pipe,
  path = R.path,
  map = R.map;

const myData = {
  "one": {
    "two": {
      "id": "1",
      "three": [{
        "id": "33",
        "copy": [{
            "id": "1",
            "text": "lorem",
            "answer": [],
          },
          {
            "id": "2",
            "text": "ipsum",
            "answer": [{
              "id": 1,
              "class": "science"
            }]
          },
          {
            "id": "3",
            "text": "baesun",
            "answer": [{
              "id": 2,
              "class": "reading"
            }]
          }
        ],
      }]
    }

  }
}

const result = filter(answersObj => answersObj.answers.length, flatten(pipe(
  path(['one', 'two', 'three']),
  map(step => step.copy.map(text => ({
    answers: text.answer.map(answer => ({
      class: answer.class,
    }))
  })))
)(myData)))

console.log(result);
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

Upvotes: 0

Related Questions