我有一个带有data数组的json对象。我希望用另一个数组过滤它。
我的.ts文件如下。
let filter_bank_id=[4,25,6,1];
console.log(filter_bank_id.length);
if(data.offers){
let last =data.offers.filter(offer=>{
for(let i=0;i<filter_bank_id.length;i++){
if(data.offers[i]){
let bank=filter_bank_id[i];
if(bank){
if(bank===data.offers[i].bank_id){
return offer;
}
}
}else{
alert("nodata");
}
}
});
console.log(last);
}
这里提供的是具有多个data.i的json对象。我想用filter_bank_id数组过滤它。这意味着我只想要与bank_id 4,25,6,1一样的商品,如filter_bank_id数组。但它不起作用。 问题是“let last = data.offers.filter(offer =&gt;”这一行。当我给调试器它没有进入它。我提供的json对象在下面。
offers= [
{
"image": "assets\/banks\/axi1419231043.jpg",
"offerid": 1,
"id": "1",
"bank_id": "1",
"name": "AXIS BANK",
"bank_interest": "10.99",
"emi": "2,174",
"processing_fee": "990",
"precloser_cost": "0 %",
"part_pay": "Yes",
"insurance": null,
"conditions": "",
"intrest_1_year": 0,
"intrest_total": 0
},
{
"image": "assets\/banks\/hdfc1418896652.png",
"offerid": 7,
"id": "4",
"bank_id": "4",
"name": "HDFC BANK",
"bank_interest": "10.99",
"emi": "2,174",
"processing_fee": "500",
"precloser_cost": "4.49 % for 12-24 months,3.37 % for 24-36 months,2.25 % for 36-60 months,",
"part_pay": "Yes",
"insurance": "1,362",
"conditions": "",
"intrest_1_year": 0,
"intrest_total": 0
},
{
"image": "assets\/banks\/scb1438520764.png",
"offerid": 2,
"id": "16",
"bank_id": "16",
"name": "SCB",
"bank_interest": "11.00",
"emi": "2,175",
"processing_fee": "1000",
"precloser_cost": "0 %",
"part_pay": "Yes",
"insurance": null,
"conditions": "",
"intrest_1_year": 0,
"intrest_total": 0
},
{
"image": "assets\/banks\/citi1419219218.png",
"offerid": 3,
"id": "2",
"bank_id": "2",
"name": "CITI BANK",
"bank_interest": "11.49",
"emi": "2,199",
"processing_fee": "2999",
"precloser_cost": "2 %",
"part_pay": "Yes",
"insurance": null,
"conditions": "",
"intrest_1_year": 0,
"intrest_total": 0
},
];
我的过滤器有什么问题。请帮助我。提前谢谢。
答案 0 :(得分:0)
使用像这样的过滤器
offers.filter((offer) => filter_bank_ids.indexOf(offer.bank_id) > -1)
在使用此代码段之前,请检查您的类型。我希望这会对你有所帮助