【发布时间】:2019-04-02 07:17:46
【问题描述】:
如何根据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))}
【问题讨论】:
-
如果
country_from数组有多个obj怎么办? -
@AswinKumar 感谢基于货币(“SGD”)的及时回复,必须对其进行过滤并进行组合,例如对于每个类型输入(信用,借记)和类型输出(银行),应该计算目标金额并返回如图所示的对象
-
@AswinKumar 我已经更新了代码,如果可能请分享 answer.thanks
-
我们如何在您的输出中提供
fee和rate?查找amount也是如此。我找不到您必须从原始对象中获取这些信息的位置。为什么数组里面只有一个对象? -
@iArcadia 道歉,更新了代码 obj
标签: javascript jquery arrays object