[英]how to return an array of objects returning the objects that matched some specific key:values from two different objects?
I have this two arrays of objects:我有这两个 arrays 对象:
ARRAY ONE: results of const arrayOne = props.data.map((item: any) => item);
数组一:
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);
数组二:
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.我需要将 ARRAY ONE 的键 debtor_account 的值与 ARRAY TWO 的键 account_number 的值相匹配。 If they match, return a new ARRAY TWO containing only the objects where those two keys matched.
如果它们匹配,则返回一个新的 ARRAY TWO,其中仅包含这两个键匹配的对象。 Because I will have to use the label and the currency_name from the ARRAY TWO only when the account_number/debtor_account matches.
因为只有当 account_number/debtor_account 匹配时,我才必须使用 ARRAY TWO 中的 label 和 currency_name。 These two arrays are coming from different endpoints and I am not sure how to handle this situation.
这两个 arrays 来自不同的端点,我不确定如何处理这种情况。
I would suggest creating a Set
of all debtor accounts, mapping each debtor account number from array 1 to this new Set.我建议创建一
Set
所有债务人帐户,将数组 1 中的每个债务人帐号映射到这个新集合。
We'd then use Array.filter()
on array 2 to return only the entries that have account numbers present in the debtor set.然后,我们将在数组 2 上使用
Array.filter()
以仅返回债务人集中存在帐号的条目。
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.您可以使用
Array.find()
使用更少的代码来完成此操作,但这对于大型 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; }
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.