javascript - 我如何在 React Native 中从 json 中选择一个特定的值?

标签 javascript reactjs react-native

我想得到的是选择“质量”的“url”值为hd720

组件

    componentDidMount() {

       return fetch('https://you-link.herokuapp.com/?url=https://www.youtube.com/watch?v=YGCLs9Bt_KY')
         .then((response) => response.json())
         .then((responseJson) => {
           this.setState({
             isLoading: false,
             dataSource: responseJson,
             videoUrl: responseJson[0].url
           }, function() {
           });
         })
         .catch((error) => {
           console.error(error);
         });
     }

Json调用
Link

最佳答案

您只需搜索具有匹配条件 ( Using find ) 的 JSON 数组 在这种情况下,“质量”===“hd720”

componentDidMount() {
    return fetch('https://you-link.herokuapp.com/?url=https://www.youtube.com/watch?v=YGCLs9Bt_KY')
        .then((response) => response.json())
        .then((responseJson) => {
            let url = responseJson.find(obj => obj.quality === "hd720").url
            this.setState({
                isLoading: false,
                dataSource: responseJson,
                videoUrl: url
            }, function() {});
        })
        .catch((error) => {
            console.error(error);
        });
}

关于javascript - 我如何在 React Native 中从 json 中选择一个特定的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53375754/

相关文章:

javascript - 如何使多个 document.getElementById() 在同一页面上工作?

javascript - 添加新输入:checkbox and not has value

javascript - 如何迭代 JSON 中深度嵌套的对象?

javascript - React Router Switch 路由不匹配

reactjs - 如何在 Azure 静态 Web 应用中手动部署 NextJS SPA

如果用户在 native react 构建的应用程序上隐藏了较低的硬件导航栏,则 Android 屏幕不适合

javascript - 将自定义数据添加到 firebase 数据库

javascript - 如何在 Vue Test Utils/Jest 中模拟 Web API?

javascript - 映射对象、使用状态以及通过 setState 中的函数传递值

javascript - 图片未出现在应用程序的 "release version"中