对象在javascript中不返回值

对象在javascript中不返回值,javascript,jquery,arrays,object,Javascript,Jquery,Arrays,Object,如何在javascript中基于组合获取对象值 我不知道如何根据给函数调用的输入迭代对象并获得对象值。我需要一些帮助 预期输出应如下所示 getValueCreditToBank(obj, "credit", "bank", "SGD"); getValueDebitToBank(obj, "debit", "bank", "THB"); 下面的代码获取值,但必须执行两个函数,是否有任何方法可以在单个函数调用中执行 // getValueCreditToBank(obj, "credit",

如何在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的索引:

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


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

以下是三个函数:

getValueobj、pin、pout、currency是实现所有逻辑的工具。 getValueCreditToBankobj,currency是getValueobj‘credit’、‘bank’、‘currency’的缩写。 getValueDeditToBankobj,currency是getValueobj‘借记’、‘银行’、货币的缩写。 该函数当前仅在数组中的国家/地区有一个对象时有效

逻辑很简单:

在对象数组中查找具有相应paymentIn和paymentOut对象以及相应货币的对象。 如果找到相应的对象: 用pay计算targetamounts。 返回一个新对象。 /** *用{pin=credit}和{pout=bank}简写getValue。 *@param{Object[]}obj *@param{string}货币 *@返回{Object} */ 函数getValueCreditToBankobj,货币{ 返回getValueobj,'信用','银行',货币; } /** *用{pin=debit}和{pout=bank}简写getValue。 *@param{Object[]}obj *@param{string}货币 *@返回{Object} */ 函数getValueDebitToBankobj,货币{ 返回getValueobj,'借方','银行',货币; } /** *@param{Object[]}obj *@param{string}pin *@param{string}pout *@param{string}货币 *@返回{Object} */ 函数getValueobj、pin、pout、currency{ //从函数参数中找到paymentIn和paymentOut对应的对象,初始化为未定义。 让意见,让意见; //从函数参数中找到相应的项。 const found=obj.finditem=>{ //从pin参数获取paymentIn对象。 opin=item.country\u from[0]。paymentIn.findpinItem=>pinItem.type.indexOfpin>-1; //从pout参数获取paymentOut对象。 opout=item.country\u from[0]。paymentout.findpoutItem=>poutItem.type.indexofput>-1; //如果未找到paymentIn对象、paymentOut对象或货币, //我们什么也找不到。 如果项.country_来自[0]。currency.indexOfcurrency===-1 | |!opin | |!opout{ 返回false; } 返回true; }; //如果找到对应的对象,则创建返回的对象。 如果找到{ 让targetMountWithPay=found.targetMount-found.fee*found.rate; targetAmountWithPay+=opin.fee.number*targetAmountWithPay/100; 返回{ id:found.id, 费用:parsefloat.fee, 速率:parseFloatfound.rate, 货币:货币,, paymentIn:别针, 付账:撅嘴, paymentInFee:`${opin.fee.number}${opin.fee.type}`, TargetMount:parseFloatfound.TargetMount, TargetMountWithPay:TargetMountWithPay }; } //如果不是,则返回null。 返回null; } //你给我们的测试。 console.loggetValueCreditToBankobj,“SGD”; console.loggetValueDebitToBankobj,“THB”; //如果没有发现任何东西,则进行测试。应该返回null。 console.loggetValueCreditToBankobj,“欧元”; console.loggetValueDebitToBankobj,“欧元”; console.loggetValueobj,'credit','not-a-bank','SGD'; console.loggetValueobj,'not-a-credit-nor-a-debit','bank','SGD'; //我将您的对象放在HTML中,以便在代码段中有两个不同的框架。 常量对象=[{ id:identity1, 费用:"2",, 比率:'0.5', targetAmount:'1000', 国家/地区:[{ 货币:[ 新加坡元, 美元 ], 付款方式:[{ 类型:信用卡, 速度:{ 单位:天, 编号:1 }, 费用:{ 类型:%, 数目:1.5 } },{ 类型:借方, 速度:{ 单位:天, 编号:1 }, 费用:{ 类型:%, 编号:1 } }], 付款方式:[{ 类型:银行转账 }] }] }, { id:identity2, 费用:"1",, 比率:'0.5', targetAmount:'1000', 国家/地区:[{ 货币:[ 泰铢, 美元 ], 付款方式:[{ 类型:借方, 速度:{ 单位:天, 编号:1 }, 费用:{ 类型:%, 编号:1 } } ], 付款方式:[{ 类型:银行转账 }] }] }];
如果数组中的国家/地区有多个对象怎么办?@AswinKumar感谢您基于货币的快速回复sgd必须对其进行过滤,并进行组合,如每个类型的Incredit、debit和typeoutbank,应计算目标装载量并将对象返回为shown@AswinKumar我已经更新了密码,如果可能的话,请分享答案。thanksHow我们可以在您的输出中提供费用和费率吗?找到数量也是一样的。我找不到你必须从原始对象中获取这些信息的地方。为什么数组中只有一个对象?@iArcadia道歉,更新了代码obj