Home > Enterprise >  how to return an array of objects returning the objects that matched some specific key:values from t
how to return an array of objects returning the objects that matched some specific key:values from t

Time:01-19

I have this two arrays of objects:

ARRAY ONE: results of const arrayOne = props.data.map((item: any) => item);

[
    {
        "id": 236,
        "created_at": "2023-01-18T11:42:00.000Z",
        "execution_date": "2022-10-19",
        "inpay_unique_reference": "43SFV52",
        "end_to_end_id": "qHDyAKziYHbbqP4",
        "amount": "5000000.0",
        "currency": "EUR",
        "state": "received",
        "debtor_account": "44635890652"
    },
    {
        "id": 235,
        "created_at": "2023-01-18T11:41:51.000Z",
        "execution_date": "2022-10-19",
        "inpay_unique_reference": "4KZT58F",
        "end_to_end_id": "yChn3L6WyrDe33v",
        "amount": "5000000.0",
        "currency": "EUR",
        "state": "rejected",
        "debtor_account": "44635890652"
    },
    {
        "id": 234,
        "created_at": "2023-01-18T11:41:25.000Z",
        "execution_date": "2022-10-19",
        "inpay_unique_reference": "49N5GX7",
        "end_to_end_id": "fkgRlzDNf5m05Gp",
        "amount": "100.0",
        "currency": "EUR",
        "state": "rejected",
        "debtor_account": "44635890652"
    },
    {
        "id": 233,
        "created_at": "2023-01-02T11:22:00.000Z",
        "execution_date": "2022-10-19",
        "inpay_unique_reference": "4EBFTLB",
        "end_to_end_id": "jBjnSbEBKEkL3Uh",
        "amount": "100.0",
        "currency": "EUR",
        "state": "rejected",
        "debtor_account": "45701229351"
    },
    {
        "id": 232,
        "created_at": "2023-01-02T11:21:58.000Z",
        "execution_date": "2022-10-19",
        "inpay_unique_reference": "44XT7A3",
        "end_to_end_id": "Bqzn_5QeQRgM2cM",
        "amount": "100.0",
        "currency": "EUR",
        "state": "rejected",
        "debtor_account": "45701229351"
    },
    {
        "id": 231,
        "created_at": "2023-01-02T11:21:57.000Z",
        "execution_date": "2022-10-19",
        "inpay_unique_reference": "4GP5FWD",
        "end_to_end_id": "0WTciFP_hUDa892",
        "amount": "100.0",
        "currency": "EUR",
        "state": "rejected",
        "debtor_account": "45701229351"
    },
    {
        "id": 230,
        "created_at": "2023-01-02T11:21:56.000Z",
        "execution_date": "2022-10-19",
        "inpay_unique_reference": "47AFUM5",
        "end_to_end_id": "kiYrGUqR9TVmi9J",
        "amount": "100.0",
        "currency": "EUR",
        "state": "rejected",
        "debtor_account": "45701229351"
    },
    {
        "id": 229,
        "created_at": "2023-01-02T11:17:44.000Z",
        "execution_date": "2022-10-19",
        "inpay_unique_reference": "4BYT699",
        "end_to_end_id": "xC7ebgovVERRrTQ",
        "amount": "100.0",
        "currency": "EUR",
        "state": "rejected",
        "debtor_account": "45701229351"
    }
]

ARRAY TWO: results of const arrayTwo = accountsInfo.map((account) => account);

[
    {
        "account_number": "45701229351",
        "currency_code": "EUR",
        "currency_name": "Euro",
        "balance": "9998549.0",
        "has_transactions": true
    },
    {
        "account_number": "28723609565",
        "currency_code": "GBP",
        "currency_name": "Pound Sterling",
        "balance": "0.0",
        "has_transactions": false
    },
    {
        "account_number": "29224097574",
        "currency_code": "DKK",
        "currency_name": "Danish Krone",
        "balance": "0.0",
        "has_transactions": false
    },
    {
        "account_number": "06632990033",
        "currency_code": "CAD",
        "currency_name": "Canadian Dollar",
        "balance": "0.0",
        "has_transactions": false
    },
    {
        "account_number": "60494674498",
        "currency_code": "CAD",
        "currency_name": "Canadian Dollar",
        "balance": "0.0",
        "has_transactions": false
    },
    {
        "account_number": "43887893609",
        "currency_code": "EUR",
        "currency_name": "Euro",
        "balance": "0.0",
        "has_transactions": false
    },
    {
        "account_number": "33256467724",
        "currency_code": "SEK",
        "currency_name": "Swedish Krona",
        "balance": "0.0",
        "has_transactions": false,
        "label": "Marco swedish"
    },
    {
        "account_number": "44635890652",
        "currency_code": "EUR",
        "currency_name": "Euro",
        "balance": "0.0",
        "has_transactions": false,
        "label": "Marco euro test label"
    }
]

I need to match the value of the key debtor_account of the ARRAY ONE, with the value of the key account_number of the ARRAY TWO. If they match, return a new ARRAY TWO containing only the objects where those two keys matched. Because I will have to use the label and the currency_name from the ARRAY TWO only when the account_number/debtor_account matches. These two arrays are coming from different endpoints and I am not sure how to handle this situation.

CodePudding user response:

I would suggest creating a Set of all debtor accounts, mapping each debtor account number from array 1 to this new Set.

We'd then use Array.filter() on array 2 to return only the entries that have account numbers present in the debtor set.

This approach will be efficient, since we'll only iterate each array once.

const array1 = [ { "id": 236, "created_at": "2023-01-18T11:42:00.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "43SFV52", "end_to_end_id": "qHDyAKziYHbbqP4", "amount": "5000000.0", "currency": "EUR", "state": "received", "debtor_account": "44635890652" }, { "id": 235, "created_at": "2023-01-18T11:41:51.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "4KZT58F", "end_to_end_id": "yChn3L6WyrDe33v", "amount": "5000000.0", "currency": "EUR", "state": "rejected", "debtor_account": "44635890652" }, { "id": 234, "created_at": "2023-01-18T11:41:25.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "49N5GX7", "end_to_end_id": "fkgRlzDNf5m05Gp", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "44635890652" }, { "id": 233, "created_at": "2023-01-02T11:22:00.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "4EBFTLB", "end_to_end_id": "jBjnSbEBKEkL3Uh", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "45701229351" }, { "id": 232, "created_at": "2023-01-02T11:21:58.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "44XT7A3", "end_to_end_id": "Bqzn_5QeQRgM2cM", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "45701229351" }, { "id": 231, "created_at": "2023-01-02T11:21:57.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "4GP5FWD", "end_to_end_id": "0WTciFP_hUDa892", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "45701229351" }, { "id": 230, "created_at": "2023-01-02T11:21:56.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "47AFUM5", "end_to_end_id": "kiYrGUqR9TVmi9J", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "45701229351" }, { "id": 229, "created_at": "2023-01-02T11:17:44.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "4BYT699", "end_to_end_id": "xC7ebgovVERRrTQ", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "45701229351" } ]
const array2 = [ { "account_number": "45701229351", "currency_code": "EUR", "currency_name": "Euro", "balance": "9998549.0", "has_transactions": true }, { "account_number": "28723609565", "currency_code": "GBP", "currency_name": "Pound Sterling", "balance": "0.0", "has_transactions": false }, { "account_number": "29224097574", "currency_code": "DKK", "currency_name": "Danish Krone", "balance": "0.0", "has_transactions": false }, { "account_number": "06632990033", "currency_code": "CAD", "currency_name": "Canadian Dollar", "balance": "0.0", "has_transactions": false }, { "account_number": "60494674498", "currency_code": "CAD", "currency_name": "Canadian Dollar", "balance": "0.0", "has_transactions": false }, { "account_number": "43887893609", "currency_code": "EUR", "currency_name": "Euro", "balance": "0.0", "has_transactions": false }, { "account_number": "33256467724", "currency_code": "SEK", "currency_name": "Swedish Krona", "balance": "0.0", "has_transactions": false, "label": "Marco swedish" }, { "account_number": "44635890652", "currency_code": "EUR", "currency_name": "Euro", "balance": "0.0", "has_transactions": false, "label": "Marco euro test label" } ]
 
// Create a Set containing all debtor accounts...
const debtorSet = new Set(array1.map(el => el.debtor_account));

const result = array2.filter(obj => debtorSet.has(obj.account_number));
console.log('Result:', result)
  
.as-console-wrapper { max-height: 100% !important; }

You could do this with less code using Array.find(), but this will be inefficient for large arrays.

const array1 = [ { "id": 236, "created_at": "2023-01-18T11:42:00.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "43SFV52", "end_to_end_id": "qHDyAKziYHbbqP4", "amount": "5000000.0", "currency": "EUR", "state": "received", "debtor_account": "44635890652" }, { "id": 235, "created_at": "2023-01-18T11:41:51.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "4KZT58F", "end_to_end_id": "yChn3L6WyrDe33v", "amount": "5000000.0", "currency": "EUR", "state": "rejected", "debtor_account": "44635890652" }, { "id": 234, "created_at": "2023-01-18T11:41:25.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "49N5GX7", "end_to_end_id": "fkgRlzDNf5m05Gp", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "44635890652" }, { "id": 233, "created_at": "2023-01-02T11:22:00.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "4EBFTLB", "end_to_end_id": "jBjnSbEBKEkL3Uh", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "45701229351" }, { "id": 232, "created_at": "2023-01-02T11:21:58.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "44XT7A3", "end_to_end_id": "Bqzn_5QeQRgM2cM", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "45701229351" }, { "id": 231, "created_at": "2023-01-02T11:21:57.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "4GP5FWD", "end_to_end_id": "0WTciFP_hUDa892", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "45701229351" }, { "id": 230, "created_at": "2023-01-02T11:21:56.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "47AFUM5", "end_to_end_id": "kiYrGUqR9TVmi9J", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "45701229351" }, { "id": 229, "created_at": "2023-01-02T11:17:44.000Z", "execution_date": "2022-10-19", "inpay_unique_reference": "4BYT699", "end_to_end_id": "xC7ebgovVERRrTQ", "amount": "100.0", "currency": "EUR", "state": "rejected", "debtor_account": "45701229351" } ]
const array2 = [ { "account_number": "45701229351", "currency_code": "EUR", "currency_name": "Euro", "balance": "9998549.0", "has_transactions": true }, { "account_number": "28723609565", "currency_code": "GBP", "currency_name": "Pound Sterling", "balance": "0.0", "has_transactions": false }, { "account_number": "29224097574", "currency_code": "DKK", "currency_name": "Danish Krone", "balance": "0.0", "has_transactions": false }, { "account_number": "06632990033", "currency_code": "CAD", "currency_name": "Canadian Dollar", "balance": "0.0", "has_transactions": false }, { "account_number": "60494674498", "currency_code": "CAD", "currency_name": "Canadian Dollar", "balance": "0.0", "has_transactions": false }, { "account_number": "43887893609", "currency_code": "EUR", "currency_name": "Euro", "balance": "0.0", "has_transactions": false }, { "account_number": "33256467724", "currency_code": "SEK", "currency_name": "Swedish Krona", "balance": "0.0", "has_transactions": false, "label": "Marco swedish" }, { "account_number": "44635890652", "currency_code": "EUR", "currency_name": "Euro", "balance": "0.0", "has_transactions": false, "label": "Marco euro test label" } ]

const result = array2.filter(obj => array1.find(el => el.debtor_account === obj.account_number));
console.log('Result:', result)
  
.as-console-wrapper { max-height: 100% !important; }

CodePudding user response:


for(let i = 0; i < array1.length; i  ) {
  const item1 = array1[i];
  console.log('item1: ', item1)
  let new_item = {};
  for(let j = 0; j < array2.length; j  ) {
    const item2 = array2[j];
    if(item1.debtor_account == item2.account_number) {
      new_item = {...item1, ...item2};
      break;
    }
  }
  new_array = [...new_array, new_item];
}

// new_array contains data you want
  • Related