How can I augment a JSON object with a variable's value in JavaScript? Here's my code.
addIndexedResult = (inid) => {
let url = 'https://jsonplaceholder.typicode.com/todos/'+inid
fetch(url)
.then(response => response.json())
.then(json => {
console.log(json);
// augment json with id & sort it out later in the browser
let jsonAug = {inid: {...json}}; //output: {"inid":{"userId":1,"id":7,"title":"illo expedita consequatur quia in","completed":false}}
let my_results = this.state.myResults;
my_results.push(jsonAug);
this.setState(prevState => ({
myResults: my_results
}));
})
}
Notice the output of the let jsonAug = ...
statement does not evaluate the variable, inid
.
How can I augment the json with the value of inid
?