javascript - JavaScript 中的对象没有返回值

标签 javascript jquery arrays object

如何在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
  }))
}

您在两个函数中执行几乎相同的任务,并且您没有正确使用参数,因此例如您可以通过 typein 参数确定 paymentIn 的索引:

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 参数,因为它是由函数名称确定的。

这只是一个示例,您可以用不同的方式重写,更具可读性的是为您传递来过滤和映射数组的箭头函数命名。

关于javascript - JavaScript 中的对象没有返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55468986/

相关文章:

javascript - 对组件上的隐藏属性进行动画处理的最佳方法

javascript - 打开相机返回后如何检测网络应用程序的恢复?

web-services - Web应用+移动应用的技术栈

javascript - 在 argv[2] NodeJs 之后连接参数

javascript - 下划线动态链接

javascript - 操作 $(document).ready 上的 datePicker CSS

javascript - 如何取消设置 CSS 值?

javascript - 仅应用jquery mobile 页面的一部分?

javascript - 当过滤器上没有可用项目时,如何在 shuffle.js 上显示消息?

Python 命名元组切片