Mapping ids from one array to other in js

K

K S

Guest
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:[])

Continue reading...
 
Top