0π
β
then you need to define a global variable (idx
for instance) to iterate through this temp array. then update idx value every time you call this method.
let idx = 0
async function fetchTemperature () {
const response = await fetch('sampleData.json')
const data = await response.json()
//finding temperature
var temp = data.records.map(function (elem) {
return elem.Temperature
})
console.log(temp[idx]) //returns an array instead of one value
idx = (idx+1)%temp.length
}
0π
map returns an array.
So using join and passing the new line character as the delimitter can output what you want.
async function fetchTemperature(){
const response = await fetch("sampleData.json");
const data = await response.json();
//finding temperature
var temp = data.records.map(function(elem){
return elem.Temperature;
});
console.log(temp.join('\r\n'));
}
0π
The console.log should be inside the map function to get 1 temperature for line if that is what you need.
Source:stackexchange.com