【问题标题】:Get the object value based on input objects in javascript根据javascript中的输入对象获取对象值
【发布时间】:2019-03-29 08:46:41
【问题描述】:

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

例如,带有 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"
            }
       }]
    }]
}]

【问题讨论】:

  • 对预期输出感到困惑,这真的是预期的还是未格式化的?
  • 但是为什么带有 'id: insta ...' 的第二行是结果的一部分?它有一个 paymentIn 类型的借方,而输入要求贷方?
  • @wentjun 感谢您的及时回复,将更新更改
  • @Harish 感谢您的及时回复,我应该将输出作为 paymentin 值和 paymentout 值发送到相应的源,type_in 和 type_out
  • @sowmiya 我已经发布了我的回复。它应该工作。我会尝试格式化它

标签: javascript arrays node.js object


【解决方案1】:

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

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个条件的元素将得到字符串结果。


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

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];

}

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2021-10-24
    • 2018-11-19
    • 1970-01-01
    相关资源
    最近更新 更多