我如何在 react native 中从 json 中选择特定值?

bdr*_*oid 3 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.json())
         .then((responseJson) => {
           this.setState({
             isLoading: 假,
             数据源:responseJson,
             视频网址:responseJson[0].url
           }, 功能() {
           });
         })
         .catch((错误) => {
           控制台错误(错误);
         });
     }

Json 调用
链接

Har*_*sar 6

在这种情况下,您只需要使用匹配条件(使用 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);
        });
}
Run Code Online (Sandbox Code Playgroud)