首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在API信息中进行筛选,以便在React中找到与特定搜索参数的完美匹配?

如何在API信息中进行筛选,以便在React中找到与特定搜索参数的完美匹配?
EN

Stack Overflow用户
提问于 2022-05-19 18:19:00
回答 1查看 131关注 0票数 0

我有个API调用随机返回一个电影名..。代码已经设置好了,所以它只返回一个电影名。

然后,我有第二个API调用,它在第二个axios请求中使用这个电影名来查找这个特定电影标题可用的流服务。

我的问题是,第二个API调用返回大约20个选项的数组。例如,如果我搜索“大白鲨”,它会为大白鲨、所有其他的大白鲨电影以及任何其他以大白鲨为名的电影提供结果。

我的问题是,我如何过滤这些返回信息,找到一个与正确的电影标题完美匹配,并只是显示的信息?我希望你能帮忙,并确定这将是一些过滤逻辑。谢谢!我是新的反应,所以试着学习。

const optionsParams = { method: 'GET', url: 'https://mdblist.p.rapidapi.com/', params: [], headers: { 'X-RapidAPI-Host': 'mdblist.p.rapidapi.com', 'X-RapidAPI-Key': '384177d302msh9d8e58dffccp1bfa57jsnf3cea9fef042', }, }; axios .request({ ...optionsParams, params: { s: ${movieData.title}} }) .then(function (response) { console.log(response.data); const traktid = response.data.search.traktid; const traktidOptions = { ...optionsParams, params: { t: ${traktid} };axios.request(TraktidOptions).then(功能(响应)){

代码语言:javascript
复制
        const streamingServices = response.data.streams;
        setStreamingState(streamingServices);
        //console.log(streamingState);
        console.log(response.data.streams)

        let temporaryArray = [];

        for (let i = 0; i < response.data.streams.length; i++){

          temporaryArray.push(response.data.streams[i].name)
          console.log(temporaryArray);

       }
     

       setStreamingState(streamingState => [...streamingState, `${temporaryArray}`])

        if (streamingState) {
          console.log(streamingState)
        } else return false;
      })
    })`
EN

回答 1

Stack Overflow用户

发布于 2022-05-19 18:27:02

数组方法是迭代和组织数组数据的完美工具。

在这种情况下,Array.prototype.find()将帮助您查看数组,并根据您的标准返回单个项。

例如:

代码语言:javascript
复制
const yourArray = ["jaws", "not this jaws", "definitely not jaws"];

const found = yourArray.find(arrayItem => arrayItem === "jaws");

这是一个没有对象的简单示例,因此您只需根据对象属性设置条件即可。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72309386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档