如何从多个嵌套数组中提取字符串?

Roh*_*rma 0 javascript arrays

我有多个数组。现在我尝试合并所有数组,但没有得到我期望的结果。

我只需要检索按字母顺序排列的列表中的所有主题。

这是我的代码:-

result = [
    {
        "id": 1,
        "name": "Pre Clinical",
        "subject": [
            {
                "id": 1,
                "name": "Human Physiology",
                "topic": [
                    {
                        "id": 1,
                        "name": "Topic 1"
                    },
                    {
                        "id": 2,
                        "name": "Topic 2"
                    },
                    {
                        "id": 3,
                        "name": "Topic 3"
                    },
                    {
                        "id": 4,
                        "name": "Topic 4"
                    }
                ]
            },
            {
                "id": 15,
                "name": "Anatomy",
                "topic": []
            },
            {
                "id": 16,
                "name": "Biochemistry",
                "topic": []
            }
        ]
    },
    {
        "id": 2,
        "name": "Para Clinical",
        "subject": [
            {
                "id": 2,
                "name": "Pathology",
                "topic": [
                    {
                        "id": 5,
                        "name": "Topic 5"
                    },
                    {
                        "id": 6,
                        "name": "Topic 6"
                    },
                    {
                        "id": 7,
                        "name": "Topic 7"
                    },
                    {
                        "id": 8,
                        "name": "Topic 8"
                    },
                    {
                        "id": 9,
                        "name": "Topic 9"
                    }
                ]
            },
            {
                "id": 9,
                "name": "Forensic Medicine & Toxicology",
                "topic": []
            },
            {
                "id": 17,
                "name": "Microbiology",
                "topic": []
            }
        ]
    },
    {
        "id": 3,
        "name": "Clinical",
        "subject": [
            {
                "id": 3,
                "name": "Ophthalmology",
                "topic": [
                    {
                        "id": 10,
                        "name": "Topic 10"
                    }
                ]
            },
            {
                "id": 4,
                "name": "Preventive and Social Medicine",
                "topic": []
            },
            {
                "id": 5,
                "name": "Radiology",
                "topic": []
            },
            {
                "id": 6,
                "name": "ENT",
                "topic": []
            },
            {
                "id": 7,
                "name": "Medicine",
                "topic": []
            },
            {
                "id": 11,
                "name": "Community Medicine",
                "topic": []
            },
            {
                "id": 12,
                "name": "Psychiatry",
                "topic": []
            },
            {
                "id": 18,
                "name": "Anaesthesiology",
                "topic": []
            },
            {
                "id": 21,
                "name": "Otorhinolaryngology",
                "topic": []
            },
            {
                "id": 24,
                "name": "Orthopaedics",
                "topic": []
            },
            {
                "id": 25,
                "name": "Paediatrics",
                "topic": []
            },
            {
                "id": 26,
                "name": "Dermatology & Venereology",
                "topic": []
            },
            {
                "id": 27,
                "name": "Obstetrics & Gynaecology",
                "topic": []
            },
            {
                "id": 28,
                "name": "Pharmacology",
                "topic": []
            },
            {
                "id": 29,
                "name": "Surgery Essence",
                "topic": []
            }
        ]
    }
]

const subjectResult = [];
for(var i = 0; i < result.length; i++){
      subjectResult.push(result[i].subject.name);                           
}
console.log(subjectResult);
Run Code Online (Sandbox Code Playgroud)

这将返回:

[
  undefined,
  undefined,
  undefined
]
Run Code Online (Sandbox Code Playgroud)

...但我想得到:

[
  "Anaesthesiology",
  "Anatomy",
  "Biochemistry",
  "Community Medicine",
  "Dermatology & Venereology",
  "ENT",
  "Forensic Medicine & Toxicology",
  "Human Physiology",
  "Medicine",
  "Microbiology",
  "Obstetrics & Gynaecology",
  "Ophthalmology",
  "Orthopaedics",
  "Otorhinolaryngology",
  "Paediatrics",
  "Pathology",
  "Pharmacology",
  "Preventive and Social Medicine",
  "Psychiatry",
  "Radiology",
  "Surgery Essence"
]
Run Code Online (Sandbox Code Playgroud)

tri*_*cot 6

您可以使用flatMap

const subjectResult = result.flatMap(({subject}) => subject.map(({name}) => name));
Run Code Online (Sandbox Code Playgroud)

如果您需要对其进行排序,请链接sort()对它的调用:

const subjectResult = result.flatMap(({subject}) => subject.map(({name}) => name));
Run Code Online (Sandbox Code Playgroud)