javascript - 对象在javascript中没有返回值
问题描述
如何根据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))}
解决方案
我在您的代码中看到的第一个问题是您在所有数组上进行了映射,但您只更改了一些元素。
也许您没有注意到这一点,因为您设置的过滤器正在传递所有数组元素,但如果没有,您将在最终数组中看到许多未定义的元素。
因此,我建议您更新引入过滤器的代码:
function getValueCreditToBank(provider, typein, typeout, source){
return provider
.filter(item => (item.country_from[0].paymentIn[0].type == typein
&& item.country_from[0].currency.includes(source)
&& item.country_from[0].paymentOut[0].type == typeout))
.map(item => ({
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
}))
}
您在两个函数中执行几乎相同的任务,并且您没有正确使用参数,因此例如您可以paymentIn
通过typein
参数确定索引:
function getValueToBank(provider, typein, typeout, source){
const paymentInIndex = typein === 'credit' ? 0 : 1;
return provider
.filter(item => (item.country_from[0].paymentIn[paymentInIndex].type == typein
&& item.country_from[0].currency.includes(source)
&& item.country_from[0].paymentOut[0].type == typeout))
.map(item => ({
paymentIn: typein,
paymentOut: typeout,
paymentInFee: item.country_from[0].paymentIn[paymentInIndex].fee.number + "%",
payInSpeed: item.country_from[0].paymentIn[paymentInIndex].speed.number + "days",
...item
}))
}
然后你可以实现你的功能:
function getValueCreditToBank(provider, typeout, source){
return getValueToBank(provider, 'credit', typeout, source)
.map(y=>({
...y,
targetAmountwithPay: y.targetAmount + y.country_from[0].paymentIn[0].fee.number*y.targetAmount/100
}))
}
function getValueDebitToBank(provider, typeout, source){
return getValueToBank(provider, 'debit', typeout, source)
}
因此,您从原始函数中删除typein
参数,因为它由函数名称确定。
这只是一个例子,你可以用不同的方式重写,一个更易读的方法是给你传递给过滤和映射数组的箭头函数命名。
推荐阅读
- java - 如何在没有小时的情况下解析 HH:mm:ss?
- html - 使剪辑路径与可拖动图像一起使用
- java - 避免方法调用 getLatitude 可能产生 java.Lang.NullPointerException
- javascript - JS:303后重定向查看其他
- python - 如何克服“ValueError:错误传递的项目数 2,放置意味着 1”错误?
- html - 如何用元素正确包装 div 元素
- javascript - RxJS:大型可观察数组上的块和延迟
- c# - 如何将图像的服务器路径分配给 img src
- python - 轮廓元组的长度必须为 2 或 3,否则 opencv 会再次更改其 cv.findcontours 签名
- dart - Flutter:TextFormField 未在 onSaved/onTap/onSubmitteed 上提交