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

问题描述 投票:0回答:2

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

我正在使用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"}]}]
javascript ramda.js
2个回答
3
投票

获取带有路径的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/[email protected]"></script>

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>
© www.soinside.com 2019 - 2024. All rights reserved.