作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道如何修改 Javascript 中的特定对象,
我能够根据过滤器获取对象,但如何将未过滤 ID 的“`` 'in'='bank' 和 'out'='bank' 添加到过滤对象的末尾
如果in
和
out
是
'bank'
,
'bank'
应该是默认值,in
是 'credit'
而 out
是 'bank'
而 id
是 'trans'
,然后过滤掉'trans'
,把剩下的in
,out
,bank
和 ids
如图所示。
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/
我是一名优秀的程序员,十分优秀!