K S
K S

Reputation: 274

Mapping ids from one array to other in js

I'm trying to map all the vins from vins array to rawResponse array based on ewr_fr_id.

Below is my code:

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 } ];

This is want I am trying:

  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
  }
}

My expected result is:

   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'] } ];

Basically I am trying to modify rawResponse array to add vins array if the ewr_fr_id is same as that in vins arrays, if there is no match then add an empty array (vins:[])

Upvotes: 2

Views: 74

Answers (4)

Sachith Rukshan
Sachith Rukshan

Reputation: 351

You can use a map function on rawRequest variable to start mapping each item and then run a for loop on vins to find the vins to be added.

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

Upvotes: 1

Nikhil
Nikhil

Reputation: 6641

Use map() to transform each item in rawResponse. Filter vins by ewr_fr_id using filter(). And use map() on the filtered results to only return 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);

Upvotes: 0

f278f1b2
f278f1b2

Reputation: 311

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

Upvotes: 2

IceMetalPunk
IceMetalPunk

Reputation: 5556

You can use a combination of forEach and reduce here:

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

Upvotes: 0

Related Questions