0

        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.

Berkay O.
  • 65
  • 6

2 Answers2

1

Try this,

data[i][whatYouWant]
Vivek Bani
  • 3,703
  • 1
  • 9
  • 18
1

It should actually be

data[i][whatYouWant]
Simeon
  • 21
  • 1
  • 4