gpt4 book ai didi

javascript - 如何在javascript中过滤和添加剩余的obj

转载 作者:行者123 更新时间:2023-11-30 11:06:08 25 4
gpt4 key购买 nike

我想知道如何修改 Javascript 中的特定对象,

我能够根据过滤器获取对象,但如何将未过滤 ID 的“`` 'in'='bank' 和 'out'='bank' 添加到过滤对象的末尾

如果 inout'bank'

'bank' 应该是默认值,in'credit'out'bank'id'trans',然后过滤掉'trans',把剩下的inoutbankids 如图所示。

function getValue(send,receive, id){
const temp = obj.map(e => Object.entries(e).map(([k, val]) => val)).flat(3)
this.selectedProviderList = temp.filter(x=>x.in== send && x.in ==receive && x.id==id);
}

getValue("credit", "bank", "trans");
var obj = [{
"btob": [{
"id": "trans",
"in": "bank",
"out": "bank",
"value": 10
},{
"id": "fund",
"in": "bank",
"out": "bank",
"value": 10
}],
"ctob": [{
"id": "trans",
"in": "credit",
"out": "bank",
"value": 20
},{
"id": "fund",
"in": "credit",
"out": "bank",
"value": 10
}]
}]

例如,如果 in'credit' 并且 out'bank' 并且id'trans'

// Expected Output
[
{
"id": "trans",
"in": "credit",
"out": "bank",
"value": 20
},
{
"id": "fund",
"in": "bank",
"out": "bank",
"value": 10
}
]

// get the values if 'in' is 'debit' and 'out' is 'bank' and id is "fund"

[{
"id": "trans",
"in": "bank",
"out": "bank",
"value": 10
},{
"id": "fund",
"in": "debit",
"out": "bank",
"value": 10
}]

// get the values if 'in' is 'bank' and 'out' is bank and id is "trans"

[{
"id": "trans",
"in": "bank",
"out": "bank",
"value": 10
},{
"id": "fund",
"in": "bank",
"out": "bank",
"value": 10
}]

最佳答案

如果你能够转换你的对象,那么一步过滤器就可以做到这一点

var obj = [
{
"id": "trans",
"in": "bank",
"out": "bank",
"value": 10
},
{
"id": "fund",
"in": "bank",
"out": "bank",
"value": 10
},
{
"id": "trans",
"in": "credit",
"out": "bank",
"value": 20
},
{
"id": "fund",
"in": "credit",
"out": "bank",
"value": 10
}
]
// get the values if in is 'credit' and out is 'bank' and id is 'trans'
let resOne=obj.filter((givenObj)=>{
if(givenObj.in=="credit"&&givenObj.out=="bank"&&givenObj.id == "trans"){
return givenObj
}
})
console.log(resOne)


//// get the values if 'in' is 'debit' and 'out' is 'bank' and id is "fund"
let resTwo=obj.filter((givenObj)=>{
if(givenObj.in=="debit"&&givenObj.out=="bank"&&givenObj.id == "fund"){
return givenObj
}
})
console.log(resTwo)


//// get the values if 'in' is 'bank' and 'out' is bank and id is "trans"
let resThree=obj.filter((givenObj)=>{
if(givenObj.in=="bank"&&givenObj.out=="bank"&&givenObj.id == "trans"){
return givenObj
}
})
console.log(resThree)

关于javascript - 如何在javascript中过滤和添加剩余的obj,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55529216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com