get data from fetch api into variable
<script>
function getFromAPI(url, callback){
var obj;
fetch(url)
.then(res => res.json())
.then(data => obj = data)
.then(() => callback(obj))
}
getFromAPI('https://jsonplaceholder.typicode.com/posts', getData);
function getData(arrOfObjs){
var results = "";
arrOfObjs.forEach( (x) => {
results += "<p> Id: " + x.id + "<ul>"
Object.keys(x).forEach( (p) => {
results += "<li>" + (p + ": " + x[p]) + "</li>";
});
results += "</ul> </p> <hr>"
})
results += "";
document.getElementById("myDiv").innerHTML = results;
}
</script>6665651554
2097
Also in JavaScript:
- pull out only text from element javascript
- angular pipe to capitalize first letter
- set localstorage
- javascript send post data with ajax
- javascript csv upload and parse
- jquery if is visible
- change elements class javascript
- mariadb JSON_ARRAYAGG does not exist
- Node require module
- jest spy on class method
- To set the text of button using Jquery
- javascript after split then trim whitespace
- callout react
- javascript ... operator three dots
- how to filter data from mongodb date in reactjs
- delete from list javascript
- javascript function expression
- remove double slash from url javascript
- insert element at beginning of array javascript
- settimeout es6
- create responsive navbar without javascript
- what foreach method returns in javascript
- 'React' must be in scope when using JSX react/react-in-jsx-scope
- nexusjs file upload