Chartjs-Javascript instead of mapping to an array, returning one value at a time instead of a list of data?

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.

Leave a comment