簡體   English   中英

在js中將id從一個數組映射到另一個數組

[英]Mapping ids from one array to other in js

我正在嘗試 map 基於 ewr_fr_id 從 vins 數組到 rawResponse 數組的所有 vins。

下面是我的代碼:

const rawResponse=[ { ewr_fr_id: '303',
    ewr_id: 102,
    yrq: '182',
    file_sequence: '1',
    fr_sequence: '102-182-1',
    manufacturer_name: 'Teryerter',
    report_category_cd: 'L',
    report_category_description: 'LightVehicle',
    date_qtr: '2018Q2',
    file_version: 1,
    file_type_cd: 'F',
    application_cd: 'S',
    application_description: 'SINGLE MODEL',
    file_name: 'SFNLIBERTY4X4-0404-10-06573.PDF',
    ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06573.PDF',
    model_or_platform: 'VOLTA',
    model_year: '2016',
    is_attachment: 'false',
    cmpt_01: '10',
    cmpt_descr_01: 'PowerTrain',
    cmpt_02: null,
    cmpt_descr_02: null,
    cmpt_03: null,
    cmpt_descr_03: null,
    cmpt_04: null,
    cmpt_descr_04: null,
    cmpt_05: null,
    cmpt_descr_05: null },
  { ewr_fr_id: '303',
    ewr_id: 102,
    yrq: '182',
    file_sequence: '1',
    fr_sequence: '102-182-1',
    manufacturer_name: 'Teryerter',
    report_category_cd: 'L',
    report_category_description: 'LightVehicle',
    date_qtr: '2018Q2',
    file_version: 1,
    file_type_cd: 'F',
    application_cd: 'S',
    application_description: 'SINGLE MODEL',
    file_name: 'SFNLIBERTY4X4-0404-10-06573.PDF',
    ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06573.PDF',
    model_or_platform: 'VOLTA',
    model_year: '2016',
    is_attachment: 'false',
    cmpt_01: '10',
    cmpt_descr_01: 'PowerTrain',
    cmpt_02: null,
    cmpt_descr_02: null,
    cmpt_03: null,
    cmpt_descr_03: null,
    cmpt_04: null,
    cmpt_descr_04: null,
    cmpt_05: null,
    cmpt_descr_05: null },
  { ewr_fr_id: '302',
    ewr_id: 102,
    yrq: '182',
    file_sequence: '1',
    fr_sequence: '102-182-1',
    manufacturer_name: 'Teryerter',
    report_category_cd: 'L',
    report_category_description: 'LightVehicle',
    date_qtr: '2018Q2',
    file_version: 1,
    file_type_cd: 'F',
    application_cd: 'S',
    application_description: 'SINGLE MODEL',
    file_name: 'SFNLIBERTY4X4-0404-10-06572.PDF',
    ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06572.PDF',
    model_or_platform: 'VOLTA',
    model_year: '2016',
    is_attachment: 'false',
    cmpt_01: '10',
    cmpt_descr_01: 'PowerTrain',
    cmpt_02: null,
    cmpt_descr_02: null,
    cmpt_03: null,
    cmpt_descr_03: null,
    cmpt_04: null,
    cmpt_descr_04: null,
    cmpt_05: null,
    cmpt_descr_05: null },
  { ewr_fr_id: '302',
    ewr_id: 102,
    yrq: '182',
    file_sequence: '1',
    fr_sequence: '102-182-1',
    manufacturer_name: 'Teryerter',
    report_category_cd: 'L',
    report_category_description: 'LightVehicle',
    date_qtr: '2018Q2',
    file_version: 1,
    file_type_cd: 'F',
    application_cd: 'S',
    application_description: 'SINGLE MODEL',
    file_name: 'SFNLIBERTY4X4-0404-10-06572.PDF',
    ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06572.PDF',
    model_or_platform: 'VOLTA',
    model_year: '2016',
    is_attachment: 'false',
    cmpt_01: '10',
    cmpt_descr_01: 'PowerTrain',
    cmpt_02: null,
    cmpt_descr_02: null,
    cmpt_03: null,
    cmpt_descr_03: null,
    cmpt_04: null,
    cmpt_descr_04: null,
    cmpt_05: null,
    cmpt_descr_05: null } ];

const fieldReportIds=[ '303', '303', '302', '302' ];

const vins=[ { vin: '2FMPK3J84FBB73425', ewr_fr_id: 303 },
  { vin: 'USELESS', ewr_fr_id: 302 },
  { vin: 'USELESS2', ewr_fr_id: 302 },
  { vin: 'VIN5801', ewr_fr_id: 303 } ];

這是我想嘗試的:

  for (const row in rawResponse) {
    getVins = await db.select(context.dbClient, q.getVinsQuery, [rawResponse[row].ewr_fr_id]);
    if (vins.length > 0 ) {
    rawResponse[row].vins =  _.map(rawResponse, vins, 'ewr_fr_id');
  }
  else 
  {
    rawResponse[row].vins = []; // if there is no vins then add vins: [] to the response
  }
}

我的預期結果是:

   rawResponse=[ { ewr_fr_id: '303',
    ewr_id: 102,
    yrq: '182',
    file_sequence: '1',
    fr_sequence: '102-182-1',
    manufacturer_name: 'Teryerter',
    report_category_cd: 'L',
    report_category_description: 'LightVehicle',
    date_qtr: '2018Q2',
    file_version: 1,
    file_type_cd: 'F',
    application_cd: 'S',
    application_description: 'SINGLE MODEL',
    file_name: 'SFNLIBERTY4X4-0404-10-06573.PDF',
    ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06573.PDF',
    model_or_platform: 'VOLTA',
    model_year: '2016',
    is_attachment: 'false',
    cmpt_01: '10',
    cmpt_descr_01: 'PowerTrain',
    cmpt_02: null,
    cmpt_descr_02: null,
    cmpt_03: null,
    cmpt_descr_03: null,
    cmpt_04: null,
    cmpt_descr_04: null,
    cmpt_05: null,
    cmpt_descr_05: null,
    vins:['VIN5801', '2FMPK3J84FBB73425']},
  { ewr_fr_id: '303',
    ewr_id: 102,
    yrq: '182',
    file_sequence: '1',
    fr_sequence: '102-182-1',
    manufacturer_name: 'Teryerter',
    report_category_cd: 'L',
    report_category_description: 'LightVehicle',
    date_qtr: '2018Q2',
    file_version: 1,
    file_type_cd: 'F',
    application_cd: 'S',
    application_description: 'SINGLE MODEL',
    file_name: 'SFNLIBERTY4X4-0404-10-06573.PDF',
    ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06573.PDF',
    model_or_platform: 'VOLTA',
    model_year: '2016',
    is_attachment: 'false',
    cmpt_01: '10',
    cmpt_descr_01: 'PowerTrain',
    cmpt_02: null,
    cmpt_descr_02: null,
    cmpt_03: null,
    cmpt_descr_03: null,
    cmpt_04: null,
    cmpt_descr_04: null,
    cmpt_05: null,
    cmpt_descr_05: null,
    vins:['VIN5801', '2FMPK3J84FBB73425']},
  { ewr_fr_id: '302',
    ewr_id: 102,
    yrq: '182',
    file_sequence: '1',
    fr_sequence: '102-182-1',
    manufacturer_name: 'Teryerter',
    report_category_cd: 'L',
    report_category_description: 'LightVehicle',
    date_qtr: '2018Q2',
    file_version: 1,
    file_type_cd: 'F',
    application_cd: 'S',
    application_description: 'SINGLE MODEL',
    file_name: 'SFNLIBERTY4X4-0404-10-06572.PDF',
    ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06572.PDF',
    model_or_platform: 'VOLTA',
    model_year: '2016',
    is_attachment: 'false',
    cmpt_01: '10',
    cmpt_descr_01: 'PowerTrain',
    cmpt_02: null,
    cmpt_descr_02: null,
    cmpt_03: null,
    cmpt_descr_03: null,
    cmpt_04: null,
    cmpt_descr_04: null,
    cmpt_05: null,
    cmpt_descr_05: null,
    vins:['USELESS', 'USELESS2']},
  { ewr_fr_id: '302',
    ewr_id: 102,
    yrq: '182',
    file_sequence: '1',
    fr_sequence: '102-182-1',
    manufacturer_name: 'Teryerter',
    report_category_cd: 'L',
    report_category_description: 'LightVehicle',
    date_qtr: '2018Q2',
    file_version: 1,
    file_type_cd: 'F',
    application_cd: 'S',
    application_description: 'SINGLE MODEL',
    file_name: 'SFNLIBERTY4X4-0404-10-06572.PDF',
    ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06572.PDF',
    model_or_platform: 'VOLTA',
    model_year: '2016',
    is_attachment: 'false',
    cmpt_01: '10',
    cmpt_descr_01: 'PowerTrain',
    cmpt_02: null,
    cmpt_descr_02: null,
    cmpt_03: null,
    cmpt_descr_03: null,
    cmpt_04: null,
    cmpt_descr_04: null,
    cmpt_05: null,
    cmpt_descr_05: null,
    vins:['USELESS', 'USELESS2'] } ];

基本上我正在嘗試修改 rawResponse 數組以添加 vins 數組,如果 ewr_fr_id 與 vins arrays 中的相同,如果沒有匹配則添加一個空數組(vins:[])

rawResponse.map(item => ({
  ...item,
  vins: vins
    .filter(vin => vin.ewr_fr_id.toString() === item.ewr_fr_id.toString())
    .map(vin => vin.vin)
}))

您可以在 rawRequest 變量上使用 map function 開始映射每個項目,然后在 vins 上運行 for 循環以查找要添加的 vins。

const res = rawResponse.map(item => {
    let myVins = [];
    vins.forEach(vin => {
        if(vin.ewr_fr_id===Number(item.ewr_fr_id)){
            myVins.push(vin.vin);
        }
    });
    item.vins = myVins;
    return item;
})

您可以在此處使用forEachreduce的組合:

rawResponse.forEach(response => {
  response.vins = vins.reduce((vinsArray, vin) => {
    if (+vin.ewr_fr_id === +response.ewr_fr_id) {
      vinsArray.push(vin.vin);
    }
    return vinsArray;
  }, []);
});

使用map()轉換rawResponse中的每個項目。 使用filter()ewr_fr_id過濾vins 並在過濾后的結果上使用map()以僅返回vin

 const rawResponse = [{ ewr_fr_id: '303', ewr_id: 102, yrq: '182', file_sequence: '1', fr_sequence: '102-182-1', manufacturer_name: 'Teryerter', report_category_cd: 'L', report_category_description: 'LightVehicle', date_qtr: '2018Q2', file_version: 1, file_type_cd: 'F', application_cd: 'S', application_description: 'SINGLE MODEL', file_name: 'SFNLIBERTY4X4-0404-10-06573.PDF', ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06573.PDF', model_or_platform: 'VOLTA', model_year: '2016', is_attachment: 'false', cmpt_01: '10', cmpt_descr_01: 'PowerTrain', cmpt_02: null, cmpt_descr_02: null, cmpt_03: null, cmpt_descr_03: null, cmpt_04: null, cmpt_descr_04: null, cmpt_05: null, cmpt_descr_05: null }, { ewr_fr_id: '303', ewr_id: 102, yrq: '182', file_sequence: '1', fr_sequence: '102-182-1', manufacturer_name: 'Teryerter', report_category_cd: 'L', report_category_description: 'LightVehicle', date_qtr: '2018Q2', file_version: 1, file_type_cd: 'F', application_cd: 'S', application_description: 'SINGLE MODEL', file_name: 'SFNLIBERTY4X4-0404-10-06573.PDF', ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06573.PDF', model_or_platform: 'VOLTA', model_year: '2016', is_attachment: 'false', cmpt_01: '10', cmpt_descr_01: 'PowerTrain', cmpt_02: null, cmpt_descr_02: null, cmpt_03: null, cmpt_descr_03: null, cmpt_04: null, cmpt_descr_04: null, cmpt_05: null, cmpt_descr_05: null }, { ewr_fr_id: '302', ewr_id: 102, yrq: '182', file_sequence: '1', fr_sequence: '102-182-1', manufacturer_name: 'Teryerter', report_category_cd: 'L', report_category_description: 'LightVehicle', date_qtr: '2018Q2', file_version: 1, file_type_cd: 'F', application_cd: 'S', application_description: 'SINGLE MODEL', file_name: 'SFNLIBERTY4X4-0404-10-06572.PDF', ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06572.PDF', model_or_platform: 'VOLTA', model_year: '2016', is_attachment: 'false', cmpt_01: '10', cmpt_descr_01: 'PowerTrain', cmpt_02: null, cmpt_descr_02: null, cmpt_03: null, cmpt_descr_03: null, cmpt_04: null, cmpt_descr_04: null, cmpt_05: null, cmpt_descr_05: null }, { ewr_fr_id: '302', ewr_id: 102, yrq: '182', file_sequence: '1', fr_sequence: '102-182-1', manufacturer_name: 'Teryerter', report_category_cd: 'L', report_category_description: 'LightVehicle', date_qtr: '2018Q2', file_version: 1, file_type_cd: 'F', application_cd: 'S', application_description: 'SINGLE MODEL', file_name: 'SFNLIBERTY4X4-0404-10-06572.PDF', ifs_document_id: '000003L041001FP-SFNLIBERTY4X4-0404-10-06572.PDF', model_or_platform: 'VOLTA', model_year: '2016', is_attachment: 'false', cmpt_01: '10', cmpt_descr_01: 'PowerTrain', cmpt_02: null, cmpt_descr_02: null, cmpt_03: null, cmpt_descr_03: null, cmpt_04: null, cmpt_descr_04: null, cmpt_05: null, cmpt_descr_05: null } ]; const vins = [{ vin: '2FMPK3J84FBB73425', ewr_fr_id: 303 }, { vin: 'USELESS', ewr_fr_id: 302 }, { vin: 'USELESS2', ewr_fr_id: 302 }, { vin: 'VIN5801', ewr_fr_id: 303 } ]; var result = rawResponse.map(item => { return {...item, "vins": vins.filter(obj => obj.ewr_fr_id == item.ewr_fr_id).map(value => value.vin) }; }); console.log(result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM