【问题标题】:How to Search a particular array from a json object in a json file in angular?如何从角度的json文件中的json对象中搜索特定数组?
【发布时间】:2021-08-12 12:53:55
【问题描述】:

我想使用 json 文件中的特定 json 对象搜索数组。

这是我的 json 文件。

{
 "data": [
      {
        "QueryID": "203972",
        "Query_Ref_No": "2019_06749",
        "Description": "cannot access files",
        "Location": "NULL"
      },
       {
        "QueryID": "203973",
        "Query_Ref_No": "2019_06751",
        "Description": "cannot access files",
        "Location": "NULL"
      }
}

下面是我的 .html 搜索代码。在这里,我使用了 ion-searchbar,它将接受输入并搜索 json 数据,并使用匹配的结果过滤数组。

<ion-searchbar
  animated
  icon="search"
  inputmode="numeric"
  showCancelButton="never"
  autocomplete="on"
  autocorrect="on"
  (ionInput)="filterItems($event)"
  (ionCancel)="onCancel()"
  placeholder="Enter Request No">
</ion-searchbar>

下面是我的 .ts 文件。在这里,当用户进入搜索栏时,将调用 filterItems(event) 函数。 我使用函数 searchData() 从 json 文件中获取整个 json 数据。 现在我想根据QUERY_REF_NO过滤数据。

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';


@Component({
  selector: 'app-connect-to-solve-details',
  templateUrl: './connect-to-solve-details.component.html',
  styleUrls: ['./connect-to-solve-details.component.scss'],
})
export class ConnectToSolveDetailsComponent implements OnInit {

  constructor(public router: Router, private http: HttpClient, private c2s: ConnectToSolveService) { }

  response: any =  []
  rev: any = []
  id: any

  searchdata: any[]
  items: any[]
  itemsarray: any[]

  result: any = []


  searchData()
  {
    this.http.get("/assets/data/C2S/concern_status.json").subscribe((searchdata: any) => {
     this.searchdata = searchdata.data;
     this.items = this.searchdata;
     //console.log(this.searchdata)
     console.log(this.items)
     })
     //return this.response
  }

  filterItems(event)
  {
    
   this.searchData();
   const val = event.target.value;
   if(val && val.trim() !== '')
    {
      this.itemsarray = this.items.filter((item) => (this.items.values["Query_Ref_No"].indexOf(val) > -1))
    console.log(this.itemsarray)
    }
    else{
      //this.isItemAvailable = false;
    }
   //console.log(event)
  }

 

}

我收到错误:ERROR TypeError: Cannot read property 'filter' of undefined

如果有人能告诉我如何搜索,那将是非常有帮助的。

提前致谢。

【问题讨论】:

  • 您必须等待searchData() 完成才能继续进行过滤。
  • 是的。 searchData() 正在提供所有 json 数据并且工作正常。然后我想从该数据中过滤
  • filterItems 函数的if 条件内console.log(this.items) 会得到什么?
  • 从 searchData() 中的console.log(this.items),我得到所有的 json 数据,例如 QueryID、Query_Ref_No、Description 等等
  • 似乎不太可能,您的错误消息表明 this.items 未定义。

标签: json angular ionic-framework


【解决方案1】:

您可以让searchDatafilterItems 函数中返回承诺和await

  searchData() {
    return this.http
     .get("/assets/data/C2S/concern_status.json").subscribe()
  }

  async filterItems(event) {
   try {
     const response = await this.searchData();
     this.searchData = response;
     this.items = response.data;
     const val = event.target.value;
       if(val && val.trim() !== '') {
         this.itemsarray = this.items.filter((item) => items["Query_Ref_No"].indexOf(val) > -1)
         console.log(this.itemsarray)
       }
    }
    catch (error) {
      console.log(error);
    }
  }

【讨论】:

  • 在 searchData() 函数中,你还没有使用 => 订阅 searchdata 后这个错误来了
  • “订阅”类型缺少“any[]”类型中的以下属性:长度、弹出、推送、连接以及另外 26 个。
猜你喜欢
  • 2020-07-21
  • 2021-12-05
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 2012-10-30
  • 2020-12-12
相关资源
最近更新 更多