Hello,
I’m trying to fetch json with plain javascript and parse the result which is working fine with the foreach when the result is an array response.
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(data => {
data.forEach(function(item) {
//fine when array
});
console.log(data)
})
But if we change the url to https://jsonplaceholder.typicode.com/users/1 then there is no array result just a single item result, forEach will fail. How could i build this to handle both single and multiple results, do i really need to check if the result is an array first?