如何在 Angular 8 中映射两个数组
how to map two arrays in Angular 8
我有两个数组 partyCtx 和 planList
partyCtx 看起来像这样:
partyCtx: Array(5)
0: {planCode: "PLN00062", planShortName: "Federal Home Loan Bank of A", employerPartyID: "", employerShortName: "", hasPayrollCenter: false, …}
1: {planCode: "PLN00125", planShortName: "Federal Home Loan Bank of B", employerPartyID: "", employerShortName: "", hasPayrollCenter: false, …}
2: {planCode: "PLN01633", planShortName: "Federal Home Loan Bank of C", employerPartyID: "", employerShortName: "", hasPayrollCenter: false, …}
3: {planCode: "PLN01819", planShortName: "Federal Home Loan Bank of D", employerPartyID: "", employerShortName: "", hasPayrollCenter: false, …}
4: {planCode: "PLN06806", planShortName: "Federal Home Loan Bank of E", employerPartyID: "", employerShortName: "", hasPayrollCenter: true, …}
length: 5
计划列表看起来像这样
planList: Array(2)
0: "PLN06806"
1: "PLN00125"
length: 2
现在我需要一个数组,你可以在其中选择计划列表,filter/map 它来自 partyctx 所以它应该看起来像
result: Array(2)
0: {planCode: "PLN06806", planShortName: "Federal Home Loan Bank of E"}
1: {planCode: "PLN00125", planShortName: "Federal Home Loan Bank of B"}
length: 2
如何获取结果数组?
只需过滤并映射即可:
const result = partyCtx.filter(p => planList.includes(p.planCode)) // filter on items with planCode in planList
.map(({planCode, planShortName}) => ({planCode, planShortName})) // map to planCode and planShortName
我有两个数组 partyCtx 和 planList
partyCtx 看起来像这样:
partyCtx: Array(5)
0: {planCode: "PLN00062", planShortName: "Federal Home Loan Bank of A", employerPartyID: "", employerShortName: "", hasPayrollCenter: false, …}
1: {planCode: "PLN00125", planShortName: "Federal Home Loan Bank of B", employerPartyID: "", employerShortName: "", hasPayrollCenter: false, …}
2: {planCode: "PLN01633", planShortName: "Federal Home Loan Bank of C", employerPartyID: "", employerShortName: "", hasPayrollCenter: false, …}
3: {planCode: "PLN01819", planShortName: "Federal Home Loan Bank of D", employerPartyID: "", employerShortName: "", hasPayrollCenter: false, …}
4: {planCode: "PLN06806", planShortName: "Federal Home Loan Bank of E", employerPartyID: "", employerShortName: "", hasPayrollCenter: true, …}
length: 5
计划列表看起来像这样
planList: Array(2)
0: "PLN06806"
1: "PLN00125"
length: 2
现在我需要一个数组,你可以在其中选择计划列表,filter/map 它来自 partyctx 所以它应该看起来像
result: Array(2)
0: {planCode: "PLN06806", planShortName: "Federal Home Loan Bank of E"}
1: {planCode: "PLN00125", planShortName: "Federal Home Loan Bank of B"}
length: 2
如何获取结果数组?
只需过滤并映射即可:
const result = partyCtx.filter(p => planList.includes(p.planCode)) // filter on items with planCode in planList
.map(({planCode, planShortName}) => ({planCode, planShortName})) // map to planCode and planShortName