如果对象包含具有空数组的键,如何删除该对象?

时间:2019-03-27 00:25:42

标签: javascript ramda.js

我有一个对象数组。我的目标是删除包含键为空数组的对象。

我正在使用ramda,但此刻正在撞墙。

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))

这是结果:

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

这是期望值:

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

2 个答案:

答案 0 :(得分:2)

使用路径获取内部three的数组,在copy属性内链接数组,并将它们投影为仅包含answer。拒绝空答案,然后将每个答案中的对象演变为仅包含class属性。

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/ramda@0.26.1"></script>

答案 1 :(得分:0)

使用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>