如何过滤和删除json查询中属性为null或为空的对象

问题描述 投票:0回答:1

我有这个 json :

{
  "meta": {
    "status": 200,
    "pagination": {
      "page": 1,
      "perPage": 15,
      "hasNext": true
    }
  },
  "data": [
    { 
      "id": "1",
      "title": "Movie title1"
      "rating": null,
      "playProviders": [
        
      ]
    },
 { 
      "id": "2",
      "title": "Movie title2"
      "rating": {
        "ratingAssessment": "7.1"
      },
      "playProviders": [
        "HBO", "Netflix"
      ]
    }, 
   ....
}

我想创建一个包含电影列表的页面,我需要获取电影,但只获取那些具有评级和 playProviders 的电影,我应该在此请求中使用哪些参数? https://api.com/movies?orderBy=views

当我过滤代码时:

programs.filter((program) => program.rating !== null);

它每页只显示几部电影,那些没有 null 的电影。例如,每页 15 个,我得到 2 个。如何过滤? (我正在使用反应打字稿) 我无权访问 API 代码。我需要过滤 API 返回的内容或编写查询,以便您从 API 获取已过滤的数据。

reactjs typescript
1个回答
0
投票

programs = [
{rating: 1,
playProviders: ["sf"]
},
{
rating: 4,
playProviders: []
}
]
programs.filter(function(program) {
  if (program.rating !== null && program.playProviders.length !== 0) {
    return program;
  }
})

© www.soinside.com 2019 - 2024. All rights reserved.