How to get the object value based on combinations in javascript?
I Don't know how to iterate over the object based on inputs given to the function call and get the object value. Need some help.
Expected output should be as shown below.
getValueCreditToBank(obj, "credit", "bank", "SGD");
getValueDebitToBank(obj, "debit", "bank", "THB");
Below code gets the value but have to do two functions, is there any method to do in single function call,
// 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
}))
}
Example of the object:
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"
}]
}]
}]
Expected Output:
//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))}
country_fromarray has more than one obj?feeandratein your output? The same for finding theamount. I don't find where you have to take these information from your original object. And why are there array with only one object inside?