如何循环遍历 Javascript 中的数组对象?

How to loop through an object of arrays in Javascript?

对于下面的代码,

const groupbysubject = {
                    "Mathematics":
                    [
                      {totalStudents: "23", average: "78", class: "2"},
                      {totalStudents: "25", average: "80", class: "3"}
                    ],
                    "English":
                    [
                      {totalStudents: "33", average: "98", class: "2"},
                      {totalStudents: "35", average: "99", class: "3"}
                    ],
                    "Science":
                    [
                      {totalStudents: "43", average: "65", class: "2"},
                    ]
                  }

                

var isEnglishPresent = Object.fromEntries(
                           Object.entries(groupbysubject).filter(
                             ([key, val])=> key.includes("English")
                            )
                        );

我想要以下输出:

"33" "98" "2" "35" "99" "3"

我已经将上面的groupbysubject对象过滤成了isEnglishPresent对象。 我如何进一步迭代 isEnglishPresent 并获得上述输出。 请帮忙

谢谢。

你想要一个 flat 列表 values 来自 English 键内的项目:

const groupbysubject = {
  "Mathematics":
  [
    {totalStudents: "23", average: "78", class: "2"},
    {totalStudents: "25", average: "80", class: "3"}
  ],
  "English":
  [
    {totalStudents: "33", average: "98", class: "2"},
    {totalStudents: "35", average: "99", class: "3"}
  ],
  "Science":
  [
    {totalStudents: "43", average: "65", class: "2"},
  ]
};

var englishData = groupbysubject.English.flatMap(item => Object.values(item));

console.log(englishData);

假设您有一个包含 N 个对象的英文数组,如下所示。

const English = [
  {totalStudents: "33", average: "98", class: "2"},
  {totalStudents: "35", average: "99", class: "3"},
  ...
]

以下是提取所有值并将其放入数组的方法。

English.map(item => Object.values(item)).flat()

这是输出

["33", "98", "2", "35", "99", "3"]