简体   繁体   English

如何从多个嵌套的arrays中提取字符串?

[英]How to extract strings from multiple nested arrays?

I have multiple arrays. Now I'm trying to merge all the arrays, but I'm not getting the result I expect.我有多个 arrays。现在我试图合并所有 arrays,但我没有得到我期望的结果。

I only need to retrieve all subjects in an alphabetical list.我只需要检索按字母顺序排列的列表中的所有主题。

This is my code:-这是我的代码:-

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

This returns:这将返回:

[
  undefined,
  undefined,
  undefined
]

...but I would like to get: ...但我想得到:

[
  "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"
]

You can use flatMap :你可以使用flatMap

const subjectResult = result.flatMap(({subject}) => subject.map(({name}) => name));

And if you need it to be sorted, chain a sort() call to it:如果您需要对它进行排序,请将sort()调用链接到它:

 const 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 = result.flatMap(({subject}) => subject.map(({name}) => name)).sort(); console.log(subjectResult);

You can use ... to spread all entries of an Array:您可以使用...传播数组的所有条目:

const subjectResult = [];
for(var i = 0; i < result.length; i++){
    subjectResult.push(...result[i].subject);
}

subjectResult.sort((a, b) => a.name < b.name? -1 : 1);

I would prefer to create a new Set from the list of subjects, to generate the unique list of subjects.我更愿意从主题列表中创建一个新的Set ,以生成唯一的主题列表。 Create a new Array from this Set and sort it.从此Set创建一个新数组并对其进行排序。 This will give the unique list of sorted subjects.这将给出唯一的排序主题列表。

Working Fiddle工作小提琴

 const 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 subjects = Array.from(new Set(result.flatMap(res => res.subject.map(sub => sub.name)))).sort(); console.log(subjects);

You can't get "name" from the array "subject"您无法从数组“主题”中获取“名称”

This should work:这应该工作:

let subjectResult = [];
for(var _result of result){
  _result.subject.map((subject) => {
    subjectResult.push(subject.name);
  })
}

console.log(subjectResult);

Full Code:完整代码:

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": []
          }
      ]
  }
]

let subjectResult = [];
for(var _result of result){
  _result.subject.map((subject) => {
    subjectResult.push(subject.name);
  })
}

console.log(subjectResult);

 const 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 final = result.flatMap((items) => items.subject.reduce((acc, cur) => { acc.push(cur.name) return acc; }, [])).sort() console.log(final);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM