3
votes

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

1
I don't see anything inherently wrong with your request. What is the error?jhc
You can use find: responseJson.find(obj => obj.quality === "hd720").urlTholle
I've tried the code but it does not work: "responseJson.find is not a function"bdroid
Try the following block of codeHarshal Bhavsar
@bdroid, if responseJson.find does not exist, then you need to debug the response before. .then(response => { console.log(response); return response.json(); })jhc

1 Answers

6
votes

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);
        });
}