Search code examples
javascriptfetch

Fetch method with the function


        let address= "https://jsonplaceholder.typicode.com/users";
        
        let element =  document.querySelector(".test");
        
        function takeData(address, element, whatYouWant) {
            
            
             fetch(address).then(response => response.json()).then(data => {
            
           for(i=0; i<data.length; i++) {
             element.innerHTML  += `<p> ${data[i].whatYouWant}  </p> ` ;
        
            }
            
        });
            
            
            
        }
       
        
        takeData(address,element, "name");

Hi, my codes in here. I create a function for reaching the fetch method. If i remove the "whatYouWant" method the function is working clearly. But in 3 values with "whatYouWant" variable so its not working. How can i use this variable in the function? I tried ${data[i]}.whatYouWant etc. but they are not working. İf you help me i will be glad.


Solution

  • Try this,

    data[i][whatYouWant]