如何过滤post api响应并仅从api响应中获取过滤后的数组对象?

发布于 2025-01-09 03:31:30 字数 1997 浏览 1 评论 0原文

我有这个 getSearchBank() post API,这是这个 api 的响应。

这个 api 为我提供了数据库中存在的所有银行

(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
  }),

现在的事情我想要的是,在管道方法中,我想迭代此帖子 api 响应中出现的数组,或者像我想检查 localStorage.getItem("bankIFSCvalue") 是否等于来自这些对象之一的 ifscCode回应,如果 ifscode 与其中之一匹配,那么我只想获取该数组对象,尽管获取了所有 8 个数组对象

就像我以简单的方式解释假设 [ localStorage.getItem("bankIFSCvalue") = johnwick ] 然后在 api 响应中我只想要第二个数组,其 ifscCode 为 johnwick

我想要这个,因为将来当我有更多银行时,会有很多如果我在调用 API 时加载 UI 中的所有数组对象,则在 UI 上加载

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-黛色若梦 2025-01-16 03:31:30

检查我创建的用于模拟您的请求的 stackblitz:

角度项目示例

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

angular project sample

胡渣熟男 2025-01-16 03:31:30

有地图

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

带过滤器是不可能的

with map

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

with filter is not possible

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文