在 Typescript 中过滤 Json 数组数据

Posted

技术标签:

【中文标题】在 Typescript 中过滤 Json 数组数据【英文标题】:Filter Json Array Data in Typescript 【发布时间】:2021-05-29 01:19:29 【问题描述】:

我最近开始研究 angular,我需要从 JSON 数组中过滤数据。 基本上我想显示 id 小于 100 的名称。下面对我不起作用。

list : any;

getOptionList()
this.callingService.getData().pipe(
  filter((l) => l.id < 100)
).subscribe(
  (l) =>  this.list = l; 
)


调用Service的Json数组

[
  
    "id":1,
    "name": "A"
  ,
  
    "id":2,
    "name": "B"
  ,
  ...
]

【问题讨论】:

【参考方案1】:
this.callingService.getData().subscribe((res: any) => 
      let filteredArr = res.filter(data => data.id < 100);
      //Here you got the filtered array.
);

它会为你工作。

【讨论】:

如果此代码有效,请标记为正确我的答案。 非常感谢您尝试了另一种方法。有效!

以上是关于在 Typescript 中过滤 Json 数组数据的主要内容,如果未能解决你的问题,请参考以下文章

在 Typescript 中解析 JSON 数组

在 TypeScript 中获取 JSON 数组的最后一个元素

如何为资产的 TypeScript 过滤器指定 tsconfig.json?

如何在 HTML 的 typescript 中迭代 JSON 数组?

typescript- 基于另一个数组过滤对象数组

typescript 通过嵌套数组值过滤对象数组