how to map two arrays in Angular 8

969 views Asked by At

I have two arrays partyCtx and planList

partyCtx looks like this:

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 looks like this

planList: Array(2)
0: "PLN06806"
1: "PLN00125"
length: 2

Now i need a get an array where u pick the planlist and filter/map it from partyctx so it should look like

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

how do i get the result array?

1

There are 1 answers

0
bryan60 On BEST ANSWER

just filter and map it:

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