Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can select a specific value from json in react native?

What I'm trying to get is to select the "url" value of the "quality" is hd720

Component

    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 Call
Link

like image 831
bdroid Avatar asked Dec 21 '25 22:12

bdroid


1 Answers

You just have to search the JSON Array with the matching condition (Using find) in this case "quality" === "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);
        });
}
like image 62
Harshal Bhavsar Avatar answered Dec 24 '25 12:12

Harshal Bhavsar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!