Как получить значение объекта на основе комбинаций в javascript?
Я не знаю, как перебрать объект на основе входных данных, переданных в вызов функции, и получить значение объекта.Нужна некоторая помощь.
Ожидаемый результат должен быть таким, как показано ниже.
getValueCreditToBank(obj, "credit", "bank", "SGD");
getValueDebitToBank(obj, "debit", "bank", "THB");
Ниже код получает значение, но должен выполнять две функции, есть ли какой-либо метод, который можно выполнить в одном вызове функции,
// getValueCreditToBank(obj, "credit", "bank", "SGD");
function getValueCreditToBank(provider, typein, typeout, source){
return provider.map(item => {
if (item.country_from[0].paymentIn[0].type == typein
&& item.country_from[0].currency.includes(source)
&& item.country_from[0].paymentOut[0].type == typeout) {
return {
paymentIn: typein,
paymentOut: typeout,
paymentInFee: item.country_from[0].paymentIn[0].fee.number + "%",
payInSpeed: item.country_from[0].paymentIn[0].speed.number + "days",
...item
}
}
})
.map(y=>({
...y,
targetAmountwithPay: y.targetAmount + y.country_from[0].paymentIn[0].fee.number*y.targetAmount/100
}))
}
// getValueDebitToBank(obj, "debit", "bank", "SGD");
function getValueDebitToBank(provider, typein, typeout, source){
return provider.map(item => {
if (item.country_from[0].paymentIn[1].type == typein
&& item.country_from[0].currency.includes(source)
&& item.country_from[0].paymentOut[0].type == typeout) {
return {
paymentIn: typein,
paymentOut: typeout,
paymentInFee: item.country_from[0].paymentIn[1].fee.number + "%",
payInSpeed: item.country_from[0].paymentIn[1].speed.number + "days",
...item
}
}
})
.map(y=>({
...y,
targetAmountwithPay: y.targetAmount + y.country_from[0].paymentIn[0].fee.number*y.targetAmount/100
}))
}
Пример объекта:
var obj = [{
"id": "identity1",
"fee": '2',
"rate": '0.5',
"targetAmount": '1000',
"country_from": [{
"currency": [
"SGD",
"USD"
],
"paymentIn": [{
"type": "credit",
"speed": {
"unit": "days",
"number": "1"
},
"fee": {
"type": "%",
"number": "1.5"
}
},{
"type": "debit",
"speed": {
"unit": "days",
"number": "1"
},
"fee": {
"type": "%",
"number": "1"
}
}],
"paymentout":[{
"type":"bank"
}]
}]
},
{
"id": "identity2",
"fee": '1',
"rate": '0.5',
"targetAmount": '1000',
"country_from": [{
"currency": [
"THB",
"USD"
],
"paymentIn": [{
"type": "debit",
"speed": {
"unit": "days",
"number": "1"
},
"fee": {
"type": "%",
"number": "1"
}
}
],
"paymentout":[{
"type":"bank"
}]
}]
}]
Ожидаемый результат:
//getValue(obj, "credit", "bank", "SGD"); should return object as
{id: "identity1",
fee: '2',
rate: '0.5',
currency: SGD,
paymentIn: "credit",
paymentOut: "bank",
paymentIn Fee: 1.5%,
targetAmount: 1000,
targetAmountwithPay: 506.485 //(((targetamount-fee)*rate)+credit fee))}
//getValue(obj, "debit", "bank", "THB"); should return object as
{id: "identity2",
fee: '1',
rate: '0.5',
currency: THB,
paymentIn: "debit",
paymentOut: "bank",
paymentIn Fee: 1%,
targetAmount: 1000,
targetAmountwithPay: 504.49 //(((targetamount-fee)*rate)+credit fee))}