Home > Enterprise >  How to Pass A variable in json to get data
How to Pass A variable in json to get data

Time:10-07

I have to Get data from a json file with help of an input field data . Please Consider this symbol as my problem : ~

const inputdata = document.getElementById('myinput').value
fetch("https://jsonplaceholder.typicode.com/posts")
    .then((value) => value.json())
    .then((json) => {
      console.log(json.~)
    });

In the place of this ~ symbol i have to place my variable inputdata . how i can do that please help me

CodePudding user response:

fetch('http://example.com/movies.json')
  .then(response => response.json())
  .then(data => console.log(data));

Refer this link for more detail

CodePudding user response:

Using your original code, you could do console.log(json[inputdata])

CodePudding user response:

I'm not sure what you need, though, if you asking about dynamically accessing an object's keys, here's the solution: You have to use square brackets, as in the example below.

const json = {a: 1, b: 2}
const inputData = "a";

console.log(json[inputData]); // 1

Hope it helps. Have a nice day!

  • Related