获取javascript中的对象值不起作用

获取javascript中的对象值不起作用,javascript,object,Javascript,Object,我想知道如何在JavaScript中获取对象的值 如果来源与对象中的国家和paymentin类型匹配,则应获得速度和费用的值。例如,带有“credit”类型的“SGD”应返回速度和费用 预期产出: id: transfer credit: 1 days 1% id: insta credit: 1 days 1.5% 首先应用过滤器以获取与国家和类型匹配的对象,然后可以应用循环过滤数组以获得所需的输出 const inputCountry=“SGD”; const inputType=

我想知道如何在JavaScript中获取对象的值

如果来源与对象中的国家和paymentin类型匹配,则应获得速度和费用的值。例如,带有“credit”类型的“SGD”应返回速度和费用

预期产出:

id: transfer  credit: 1 days 1% 
id: insta credit: 1 days 1.5%


首先应用过滤器以获取与
国家
类型
匹配的对象,然后可以应用循环过滤数组以获得所需的输出

const inputCountry=“SGD”;
const inputType=“信用”;
常量输入=
[
{
“id”:“转让”,
“国家/地区”:[
{
“货币”:[
“新加坡元”,
“美元”
],
“付款方式”:[
{
“类型”:“信用”,
“速度”:{
“单位”:“天”,
“编号”:“1”
},
“费用”:{
“类型”:“%”,
“编号”:“1”
}
}
]       
}
]
}
];
const filteredar=input.filter({country\u from})=>{
从.filter返回国家/地区({currency,paymentIn})=>{
const ispaymentIn=paymentIn.filter({type})=>{
返回类型.toLowerCase()==inputType.toLowerCase();
})
返回货币。包括(输入国家)和&ispaymentIn;
});
});
const output=filterDarr.map({id,country_from})=>{
设obj={id};
国家/地区_from.forEach({paymentIn})=>{
paymentIn.forEach({type,speed,fee})=>{
obj[type]=速度.number+速度.unit+“”+费用.number+费用.type;
});
});
返回obj;
});
控制台日志(输出)检查此代码

var source=“SGD”;
var type=“信用”;
var obj=[{“id”:“转让”、“国家/地区”:[{“货币”:[“新加坡元”、“美元”],“付款单位”:[{“类型”:“信用”、“速度”:“{“单位”:“天数”、“数量”:“1”},“费用”:“{“类型”:“%”,“数量”:“1”}]}],{“id”:“分期”,“国家/地区”:[{“货币”:[“新加坡元”、“美元”],“付款单位”:[{“类型”:“信用”、“速度”:“{“单位”:“天数”,“数量”:“1”}],“费用”:{“类型”:“%”,“数字”:“1.5”}}]}]
函数getValue(源、类型、ob){
var列表=[];
ob.forEach((cn)=>{
cn.country\u from.filter((c)=>{
让付款=c.paymentIn;
付款方式:每小时(pn=>{
如果(pn.type==类型){
const type=`id:${cn.id}${pn.type}:${pn.speed.number}${pn.speed.unit}${pn.fee.number}${pn.fee.type}`
列表、推送(类型);
}
});
});
});
退货清单;
}
//投入
var结果=getValue(来源、类型、对象);

console.log(结果)
检查请点击,
[]
stacksnippet编辑器。修复您的对象并准备一个。另外,您需要在使用它之前定义对象。代码中仍然缺少括号。请修复您的
obj
。当您请求他人帮助您编写代码时,发布可编译代码是最低要求。@adiga向所有修复代码格式的人道歉W这是什么意思?
返回c.country==source;
国家在哪里?
行中获取“无法读取未定义”的属性“filter”。filter({type})=>{
@Senthil-检查是否正在从.filter({currency,paymentIn})中提取
paymentIn
属性。
country\u
.forEach和filter以及forEach和push???
function getValue(source, type, ob) {
    var list;
    ob.forEach((cn) => {
      list = cn.country_from.filter((c) => {
        return c.currency== source;
      });
    })
    return `${type} ${list[0].paymentIn[0].credit.number}`;
  }

//inputs
var result  = getValue(source,type,obj);
var source="SGD";
var type="credit";
var obj = [{
    "id": "transfer",
    "country_from": [{
        "currency": [
            "SGD",
            "USD"
        ],
        "paymentIn": [{
            "type": "credit",
            "speed": {
                "unit": "days",
                "number": "1"
            },
            "fee": {
                "type": "%",
                "number": "1"
            }
        }]
    }]
}, {
    "id": "insta",
    "country_from": [{
        "currency": [
            "SGD",
            "USD"
        ],
        "paymentIn": [{
            "type": "credit",
            "speed": {
                "unit": "days",
                "number": "1"
            },
            "fee": {
                "type": "%",
                "number": "1.5"
            }
        }]
    }]
}]