javascript - 根据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"
}
}]
}]
}]
解决方案
我认为您在初始代码上犯了一些错误,但我想这是由于处理这么多层对象和数组的混乱造成的。这是你应该做的:
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];
}
推荐阅读
- javascript - 将原始图像数据转换为 html 中可用的数据
- django-models - 我在 django 的动态 urls 系统中有问题
- html - HTML + CSS:由于标题固定,按钮无法点击
- javascript - 如何从“Thu Aug 20 00:00:00 GMT+01:00 2020”创建正确的新 Date()
- powershell - Sharepoint PnP 列表:如何使用 Powershell 对列进行“分组”
- python - python __call__ 魔术方法是类方法还是对象方法?
- typescript - 用自己的“更多”正确覆盖固有类型 - 打字稿
- r - 使用运行长度编码 (rle) 的随机抽样
- javascript - messageReactionAdd 事件不再赘述,无需解释
- python - 字符串中重复字符的索引