获取对象数组中的所有对象

gan*_*esh 1 javascript reactjs redux react-redux

我也是新手reactjs和javascript.

在这里,我有一个对象数组,就像,

[
  {
    "id": "CSS",
    "questionCount": [
      {
        "level": "TOUGH",
        "type": "NON_CODE",
        "count": "1"
      }
    ]
  },
  {
    "id": "Backbone",
    "questionCount": [
      {
        "level": "TOUGH",
        "type": "CODE",
        "count": "2"
      },
      {
        "level": "TOUGH",
        "type": "NON_CODE",
        "count": "5"
      },
      {
        "level": "MEDIUM",
        "type": "NON_CODE",
        "count": "7"
      },
      {
        "level": "EASY",
        "type": "NON_CODE",
        "count": "6"
      }
    ]
  },
]
Run Code Online (Sandbox Code Playgroud)

现在,我想要的是拥有一个对象数组,它将包含数组中存在的所有对象questionCount.所以,它会像,

[  {
        "level": "TOUGH",
        "type": "NON_CODE",
        "count": "1"
      },  {
        "level": "TOUGH",
        "type": "CODE",
        "count": "2"
      },
      {
        "level": "TOUGH",
        "type": "NON_CODE",
        "count": "5"
      },
      {
        "level": "MEDIUM",
        "type": "NON_CODE",
        "count": "7"
      },
      {
        "level": "EASY",
        "type": "NON_CODE",
        "count": "6"
      } ]
Run Code Online (Sandbox Code Playgroud)

那么,任何人都可以帮助我吗?

Yos*_*ero 6

您可以将Array.prototype.reduce()Array.prototype.concat()一起使用:

const temp = [{"id": "CSS","questionCount": [{"level": "TOUGH","type": "NON_CODE","count": "1"}]},{"id": "Backbone","questionCount": [{"level": "TOUGH","type": "CODE","count": "2"},{"level": "TOUGH","type": "NON_CODE","count": "5"},{"level": "MEDIUM","type": "NON_CODE","count": "7"},{"level": "EASY","type": "NON_CODE","count": "6"}]},];
const result = temp.reduce((a, c) => a.concat(c.questionCount), []);

console.log(result);
Run Code Online (Sandbox Code Playgroud)