【问题标题】:Populating a JavaScript Array where elements in one array need to partially match another, and then be combined填充一个 JavaScript 数组,其中一个数组中的元素需要部分匹配另一个数组,然后合并
【发布时间】:2022-09-26 17:24:42
【问题描述】:

我有两个数组,一个包含选项代码,另一个包含它们的描述。创建包含代码及其定义的数组的最有效方法是什么?

第一个数组看起来像这样并包含代码

[ \'1CA\', \'1CD\', \'205\', \'258\', \'2K1\', \'2VH\', \'302\', \'322\', \'386\', \'3AG\', \'417\', \'423\', \'428\', \'430\', \'431\', \'441\', \'459\', \'494\', \'4BN\', \'4UR\', \'4UY\', \'502\', \'508\', \'522\', \'524\', \'534\', \'571\', \'601\', \'609\', \'644\', \'676\', \'698\', \'6VC\', \'812\', \'823\', \'845\', \'853\', \'876\', \'8KA\', \'8S1\', \'8S3\', \'8TM\', \'925\', \'992\', \'9AA\' ]

第二个数组看起来像这样,包含代码(尽管两边都有一个 S 和 A)和它的定义。

[\'S000A Dummy-SALAPA\\n\',
    \'S001A Nat.vers. not controlled as package\\n\',
    \'S0BVA Refueling until tank full\\n\',
    \'S0BWA Partial fueling\\n\',
    \'S100A Usable load increase\\n\',
    \'S102A Chassis, officials utility vehicle\\n\',
    \'S102A Reinforced brakes\\n\',
    \'S103A Four seasons tyres\\n\',
    \'S103A Aggregate protective plate\\n\',
    \'S115A Paramedic sticker NRW\\n\',
    \'S115A NRW Fire-brigade Sticker\\n\',
    \'S4BNA Fine-wood trim ash grain\\n\',
    \'S117A Banner staff, right\\n\',
    \'S160A Installation, Becker Signal Syst.LU 322\\n\',
    \'S161A Emissions standard EU5\\n\']
  • 同一组合字符串中的代码和定义?代码和定义作为数组匹配在一起?作为对象数组的代码和定义?作为一个对象?清楚你想要达到的确切最终结果。当你试图解决问题时,你走了多远?什么地方出了错?您最佳尝试的代码在哪里?除了您告诉我们的内容外,我们对您的代码或您的意图一无所知,如果我们不能告诉您您想要什么,我们将无法帮助您。
  • 你好。抱歉,我应该更清楚地了解我正在寻找的输出。我想最终得到一个包含代码和定义的对象数组。所以像let finalArray = [ {code: \'4BN\', definition: \'Fine-wood trim ash grain\'}]

标签: javascript arrays performance


【解决方案1】:

看起来唯一具有相应定义的代码是4BN

const codes = [
  '1CA', '1CD', '205', '258', '2K1', '2VH', '302', '322',
  '386', '3AG', '417', '423', '428', '430', '431', '441',
  '459', '494', '4BN', '4UR', '4UY', '502', '508', '522',
  '524', '534', '571', '601', '609', '644', '676', '698',
  '6VC', '812', '823', '845', '853', '876', '8KA', '8S1',
  '8S3', '8TM', '925', '992', '9AA',
];

const definitions = [
  'S000A Dummy-SALAPA\n',
  'S001A Nat.vers. not controlled as package\n',
  'S0BVA Refueling until tank full\n',
  'S0BWA Partial fueling\n',
  'S100A Usable load increase\n',
  'S102A Chassis, officials utility vehicle\n',
  'S102A Reinforced brakes\n',
  'S103A Four seasons tyres\n',
  'S103A Aggregate protective plate\n',
  'S115A Paramedic sticker NRW\n',
  'S115A NRW Fire-brigade Sticker\n',
  'S4BNA Fine-wood trim ash grain\n',
  'S117A Banner staff, right\n',
  'S160A Installation, Becker Signal Syst.LU 322\n',
  'S161A Emissions standard EU5\n',
];

const definitionsMap = new Map(definitions.map(s => {
  const [code, ...definition] = s.split(' ');
  return [code.substring(1, 4), definition.join(' ').trimEnd()];
}));

const codesWithDefinitions = codes.map(c => ({code: c, definition: definitionsMap.get(c)}))
                                  .filter(c => c.definition);
console.log(codesWithDefinitions);

【讨论】:

    【解决方案2】:

    使用循环和正则表达式进行比较的另一个选项,检查内联 cmets

    const codes = [
      '1CA', '1CD', '205', '258', '2K1', '2VH', '302', '322', '386',
      '3AG', '417', '423', '428', '430', '431', '441', '459', '494',
      '4BN', '4UR', '4UY', '502', '508', '522', '524', '534', '571',
      '601', '609', '644', '676', '698', '6VC', '812', '823', '845',
      '853', '876', '8KA', '8S1', '8S3', '8TM', '925', '992', '9AA'
    ]
    
    const data = [
      'S000A Dummy-SALAPA\n',
      'S001A Nat.vers. not controlled as package\n',
      'S0BVA Refueling until tank full\n',
      'S0BWA Partial fueling\n',
      'S100A Usable load increase\n',
      'S102A Chassis, officials utility vehicle\n',
      'S102A Reinforced brakes\n',
      'S103A Four seasons tyres\n',
      'S103A Aggregate protective plate\n',
      'S115A Paramedic sticker NRW\n',
      'S115A NRW Fire-brigade Sticker\n',
      'S4BNA Fine-wood trim ash grain\n',
      'S117A Banner staff, right\n',
      'S160A Installation, Becker Signal Syst.LU 322\n',
      'S161A Emissions standard EU5\n'
    ];
    
    // Resulting array
    const res = [];
    
    // Loop codes and data
    for(const c of codes) for(const e of data) {
      // Compare codes using regexp template wigh
      // S and A around the code itself and
      // push matches to resulting array
      if(new RegExp(`^S${c}A[ ]+`).test(e)) res.push({
        code: c,
        definition: e
      });
    }
    
    // Test
    console.log(res);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多