Home > Back-end >  How to filter through the post api response and get only the filtered array object from api response
How to filter through the post api response and get only the filtered array object from api response

Time:02-23

I have this getSearchBank() post API and this is the response of this api .

This api gives me all the banks which are present in the database

(8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {bankId: '616c07ca9d60c50b84e8b71b', bankName: 'IDBI', branch: 'Annapurna Road', ifscCode: 'abc121132', address: {…}, …}
1: {bankId: '61f58ff918f67f5436cebc40', bankName: 'DENA', branch: 'Gopur', ifscCode: 'denabaanak'}
2: {bankId: '61f5901318f67f5436cebc41', bankName: 'ICICI', branch: 'GopurSQUAREeee', ifscCode: 'johnwick'}
3: {bankId: '61f7d8bb3c0b195e375a5846', bankName: 'ICICII', ifscCode: 'raju'}
4: {bankId: '61fbf0e013b9e42111aa7fd4', bankName: 'PNB', branch: 'footi Kothi sq', ifscCode: 'punjab'}
5: {bankId: '61fbf20b13b9e42111aa7fd5', bankName: 'AXIS Bank', branch: 'usha nagar', ifscCode: 'axis'}
6: {bankId: '61fd2a5751498106f78469ac', bankName: 'Kishkin Goldman sachs', branch: 'Kishkingold', ifscCode: 'goldman'}
7: {bankId: '61fd2eb251498106f78469ad', bankName: 'AnnapurnaRoad', branch: 'indiabank', ifscCode: 'indiabank'}
public getSearchBank(
    ifscCode: string,
     
    ){
const postData : Create = {
  ifscCode: ifscCode,
  address: '',
  city : '',
  bankId: '',
  bankName: '',
  branch: '',
  passBookNo: ''
}
{
return this.http.post('http://localhost:9900/api/v1/bank/search',postData)
.pipe(
  filter(data => {
    var res = JSON.parse(JSON.stringify(data))
    res.ifscCode === localStorage.getItem("bankIFSCvalue")
  console.log("PIPE MAP OPERATOR CALLED!!!")
    console.log(res.body.ifscCode);
    console.log(res.body);
    return res
  }),

Now the thing I want is that in the pipe method I want to iterate over the array which is coming in this post api response , or like I want to check that if localStorage.getItem("bankIFSCvalue") is equal to ifscCode from one of these objects from the response , if the ifscode matches one of them then I want to get only that array object inspite of getting all the 8 array objects

Like if I explain in a simple manner suppose [ localStorage.getItem("bankIFSCvalue") = johnwick ] then in the api response I want only the 2nd array which has the ifscCode as johnwick

I want this because in the future when I have more banks then there will be much load on the UI if I am loading all the array objects in the UI while calling the API

CodePudding user response:

Check this stackblitz I've created to simulate your request:

angular project sample

CodePudding user response:

with map

.pipe(
        map((list) => {
          const ifscCode === localStorage.getItem("bankIFSCvalue")
          return list.find(element => element.ifscCode === ifscCode );
        })

with filter is not possible

  • Related