【问题标题】:Merge two array based on common id [duplicate]基于公共ID合并两个数组[重复]
【发布时间】:2020-08-14 15:14:03
【问题描述】:

我有这样的函数,我将 classid 传递给它

  selectedSubjects;
  classnamewithid;
  subjectNameByID;

  selectClass(selectedClass) {
          

    this.selectedSubjects = this.topicWithClassSubjectList.filter(
      (topic) => topic.class_id === selectedClass
    ); // to filter out class with same id
   

    const groups = this.selectedSubjects.reduce((acc, cur) => {
      (acc[cur.subject_id] = acc[cur.subject_id] || []).push(cur.topic_name);
      return acc;
    }, {}); // to group the array according to subject
   
// checkpoint#1
    this.selectedSubjects = Object.keys(groups).map((key) => ({
      subject_id: key,
      topics: groups[key],
    }));

  }

我的班级列表数组是这样的

{class_id: 1871, class_name: "1st"},
{class_id: 1872, class_name: "2nd"},

checkpoint#1 之后的 selectedSubjects 的最终数组是

[{"subject_id":"551","topics":["Evolution"]},{"subject_id":"711","topics":["Vector"]}]

我希望在 selectedSubjects 数组中拥有一个与每个 subject_id 相关联的 subjectName。我有一个 subjectId 数组,SubjectName 为:

{class_id: 2711, subject_id: 551, subject_name: "Biology"}

我希望 selectedSubjects 数组看起来像这样

[{"subject_id":"551", "subject_name":"biology", topics":["Evolution"]},{"subject_id":"711","subject_name":"science","topics ":["向量"]}]

【问题讨论】:

  • 那么,您的主题名称在哪里?请edit 您的问题以minimal reproducible example 的形式在代码中包含示例数据,准备运行。您可以使用Stack Snippets(工具栏中的图标类似于<>)来执行此操作。看来 Angular 和 TypeScript 对这个问题并不重要,你可以构建你的代码,这样this. 是不必要的。

标签: javascript arrays angular typescript array-merge


【解决方案1】:

没有一个最小的例子,我不完全确定你的数据是什么样的,但根据描述,这样的东西应该可以工作。

const subjectsToTopics = [{
  subject_id: 551,
  topics: ["Evolution"]
}, {
  subject_id: 711,
  topics: ["Vector"]
}];
const classList = [{
    class_id: 1871,
    class_name: "1st"
  },
  {
    class_id: 1872,
    class_name: "2nd"
  },
];
const topicWithClassSubjectList = [{
    class_id: 1871,
    subject_id: 551,
    subject_name: "Biology"
  },
  {
    class_id: 1872,
    subject_id: 551,
    subject_name: "Biology"
  }
];

function selectClass(selectedClass) {
  const classnamewithid = classList.find(
    (classes) => classes.class_id === selectedClass
  ); // to get the class name and class ID of selected class

  if (classnamewithid === null) {
    throw Error(`Class with id ${selectedClass} not found`);
  }

  const selectedSubjects = topicWithClassSubjectList.filter(
    (topic) => topic.class_id === selectedClass
  ); // to filter out class with same id


  return selectedSubjects.map((subject) => ({
    subject_id: subject.subject_id,
    subject_name: subject.subject_name,
    topics: subjectsToTopics
      .filter((entry) => entry.subject_id == subject.subject_id)
      .map((entry) => entry.topics)
      .flat()
  }));
}

console.log(selectClass(1871));
console.log(selectClass(1872));

请注意,您不会在任何地方使用classnamewithid,因此您可以将其删除。

【讨论】:

    猜你喜欢
    • 2020-07-13
    • 2015-08-27
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    相关资源
    最近更新 更多