首页 > 解决方案 > 根据javascript中的输入对象获取对象值

问题描述

我想如何根据javascript中的输入对象获取值。如果源匹配货币并且还匹配对象中的paymentin和payment类型,则获取速度和费用的值

例如带有 type_in 'credit' 和 type_out 'bank' 的 'SGD' 应该返回速度和费用

预期输出:

id: transfer  credit: 1 days 1% pay_in: pay_out: bank
id: insta debit: 1 days 1.5% pay_in: pay_out: bank

我试过但我卡住了

function getValue(source,typein,typeout,obj){
  var filterArr = source.filter(function(value){
    return value.country_from.filter(function(payin){
      const in= payin.paymentIn.filter(function(ty){
        return ty.type == typein
     })
      const out = payin.paymentIn.filter(function(ty){
        return ty.type == typeout
     })
   })
 })
}
var result  = getValue(source,type_in,type_out,obj);
//input objects
var source="SGD";
var type_in="credit";
var type_out="bank";
var obj = [{
    "id": "transfer",
    "country_from": [{
        "currency": [
            "SGD",
            "USD"
        ],
        "paymentIn": [{
            "type": "credit",
            "speed": {
                "unit": "days",
                "number": "1"
            },
            "fee": {
                "type": "%",
                "number": "1"
            }
        }],
        "paymentOut": [{
            "type": "bank",
            "speed": {
                "unit": "days",
                "number": "2"
            }
       }]
    }]
}, {
    "id": "insta",
    "country_from": [{
        "currency": [
            "SGD",
            "USD"
        ],
        "paymentIn": [{
            "type": "credit",
            "speed": {
                "unit": "days",
                "number": "1"
            },
            "fee": {
                "type": "%",
                "number": "1.5"
            }
        }],
       "paymentOut": [{
            "type": "bank",
            "speed": {
                "unit": "days",
                "number": "2"
            }
       }]
    }]
}]

标签: javascriptarraysnode.jsobject

解决方案


我认为您在初始代码上犯了一些错误,但我想这是由于处理这么多层对象和数组的混乱造成的。这是你应该做的:

const getValue = (source, typein, typeout, obj) => {

  const res = obj.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 `id: ${item['id']} credit: ${item['country_from'][0]['paymentIn'][0]['speed']['number']} days credit: ${item['country_from'][0]['paymentIn'][0]['fee']['number']}${item['country_from'][0]['paymentIn'][0]['fee']['type']} pay_in: pay_out: ${item['country_from'][0]['paymentOut'][0]['speed']['number']}`
    }
  });
  return res;

}

getValue('SGD', 'credit', 'bank', obj);

基本上,我将遍历输入数组的每个元素obj(这是您在问题上发布的那个),并且在每次迭代中,我使用 if 语句检查以下 3 个条件。

1)paymentIn type比赛typein

2)paymentOut type比赛typein

3)currency包含source

满足上述 3 个条件的元素将得到字符串结果。


编辑:要回答您关于评论的问题,如果paymentIn数组有多个对象,我们可以使用 Array.some() 检查具有该type属性的对象是否具有与typeIn.

if (item['country_from'][0]['paymentIn'].some(payment => payment['type']===typein) 
  && item['country_from'][0]['currency'].includes(source) 
  && item['country_from'][0]['paymentOut'][0]['type'] === typeout) {

  // to get the object with that typein
  const filteredPaymentIn = item['country_from'][0]['paymentIn'].filter(payment => payment['type']===typein)[0];

}

推荐阅读