[英]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.我正在尝试 map 基于 ewr_fr_id 从 vins 数组到 rawResponse 数组的所有 vins。
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:[])基本上我正在尝试修改 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)
}))
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.您可以在 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;
})
You can use a combination of forEach
and reduce
here:您可以在此处使用
forEach
和reduce
的组合:
rawResponse.forEach(response => {
response.vins = vins.reduce((vinsArray, vin) => {
if (+vin.ewr_fr_id === +response.ewr_fr_id) {
vinsArray.push(vin.vin);
}
return vinsArray;
}, []);
});
Use map() to transform each item in rawResponse
.使用map()转换
rawResponse
中的每个项目。 Filter vins
by ewr_fr_id
using filter() .使用filter()按
ewr_fr_id
过滤vins
。 And use map()
on the filtered results to only return vin
.并在过滤后的结果上使用
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.