You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据指定code数组从diagnoses对象中筛选对应诊断项?

问题描述

现有一个以诊断编码(code)为键的diagnoses对象,结构如下:

{
  M24.2: {code: 'M24.2', name: 'Disorder of ligament', latin: 'Morbositas ligamenti'},
  M51.2: {code: 'M51.2', name: 'Other specified intervertebral disc displacement', latin: 'Alia dislocatio disci intervertebralis specificata'},
  S03.5: {code: 'S03.5', name: 'Sprain and strain of joints and ligaments of other and unspecified parts of head', latin: 'Distorsio et/sive distensio articulationum et/sive…m partium aliarum sive non specificatarum capitis'},
  J10.1: {code: 'J10.1', name: 'Influenza with other respiratory manifestations, other influenza virus codeentified', latin: 'Influenza cum aliis manifestationibus respiratoriis ab agente virali codeentificato'},
  J06.9: {code: 'J06.9', name: 'Acute upper respiratory infection, unspecified', latin: 'Infectio acuta respiratoria superior non specificata'},
  F43.2: {code: 'F43.2', name: 'Adjustment disorders', latin: 'Perturbationes adaptationis'},
  H35.29: {code: 'H35.29', name: 'Other proliferative retinopathy', latin: 'Alia retinopathia proliferativa'},
  H54.7: {code: 'H54.7', name: 'Unspecified visual loss', latin: 'Amblyopia NAS'},
  J03.0: {code: 'J03.0', name: 'Streptococcal tonsillitis', latin: 'Tonsillitis (palatina) streptococcica'},
  L20: {code: 'L20', name: 'Atopic dermatitis', latin: 'Atopic dermatitis'},
  L60.1: {code: 'L60.1', name: 'Onycholysis', latin: 'Onycholysis'},
  N30.0: {code: 'N30.0', name: 'Acute cystitis', latin: 'Cystitis acuta'},
  S62.5: {code: 'S62.5', name: 'Fracture of thumb', latin: 'Fractura [ossis/ossium] pollicis'},
  Z57.1: {code: 'Z57.1', name: 'Occupational exposure to radiation'},
  Z74.3: {code: 'Z74.3', name: 'Need for continuous supervision'}
}

同时有一个存储需筛选编码的数组:

const codes = ['Z57.1', 'Z74.3', 'M51.2']

需要筛选出diagnoses中键存在于codes数组里的诊断项,预期结果为:

{
  Z57.1: {code: 'Z57.1', name: 'Occupational exposure to radiation'},
  Z74.3: {code: 'Z74.3', name: 'Need for continuous supervision'},
  M51.2: {code: 'M51.2', name: 'Other specified intervertebral disc displacement', latin: 'Alia dislocatio disci intervertebralis specificata'}
}

尝试用codes.map(d => diagnoses[d])没得到预期结果,求可行方案。

解决方法

为啥map不行?

map会返回数组,但你要的是和原结构一致的对象,所以用map只能得到一堆值的集合,没法保留原有的键名。

方案1:用reduce构建目标对象

遍历codes数组,把符合条件的键值对逐个加到新对象里:

const filteredDiagnoses = codes.reduce((acc, code) => {
  if (diagnoses[code]) { // 先判断编码是否存在,避免无效值
    acc[code] = diagnoses[code];
  }
  return acc;
}, {}); // 初始值是空对象

方案2:用Object.fromEntries(更简洁)

先把codes转成[键, 值]格式的数组,再转成对象:

const filteredDiagnoses = Object.fromEntries(
  codes.map(code => [code, diagnoses[code]]).filter(([_, value]) => value)
);

如果能保证codes里的所有编码都在diagnoses中存在,可以去掉filter简化:

const filteredDiagnoses = Object.fromEntries(
  codes.map(code => [code, diagnoses[code]])
);

方案3:普通循环构建对象

要是喜欢更直观的写法,用for循环也可以:

const filteredDiagnoses = {};
for (const code of codes) {
  if (diagnoses[code]) {
    filteredDiagnoses[code] = diagnoses[code];
  }
}

内容的提问来源于stack exchange,提问作者Julius Goddard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 03:35:26