Home > Enterprise >  How to extract single array data from API which gives two arrays of output?
How to extract single array data from API which gives two arrays of output?

Time:11-28

I am facing a problem, I can't extract data from just one array - temperature. I tried to put it into another array, but all I managed to do was put there all the data. Any advice? Thank you in advance!

Console output of the function I use: { latitude: 51.75, longitude: 19.5, generationtime_ms: 0.28395652770996094, utc_offset_seconds: 0, timezone: 'GMT', timezone_abbreviation: 'GMT', elevation: 218, hourly_units: { time: 'iso8601', temperature_2m: '°C' }, hourly: { time: [ '2022-11-26T00:00', '2022-11-26T01:00', '2022-11-26T02:00', '2022-11-26T03:00', '2022-11-26T04:00', '2022-11-26T05:00', '2022-11-26T06:00', '2022-11-26T07:00', '2022-11-26T08:00', '2022-11-26T09:00', '2022-11-26T10:00', '2022-11-26T11:00', '2022-11-26T12:00', '2022-11-26T13:00', '2022-11-26T14:00', '2022-11-26T15:00', '2022-11-26T16:00', '2022-11-26T17:00', '2022-11-26T18:00', '2022-11-26T19:00', '2022-11-26T20:00', '2022-11-26T21:00', '2022-11-26T22:00', '2022-11-26T23:00', '2022-11-27T00:00', '2022-11-27T01:00', '2022-11-27T02:00', '2022-11-27T03:00', '2022-11-27T04:00', '2022-11-27T05:00', '2022-11-27T06:00', '2022-11-27T07:00', '2022-11-27T08:00', '2022-11-27T09:00', '2022-11-27T10:00', '2022-11-27T11:00', '2022-11-27T12:00', '2022-11-27T13:00', '2022-11-27T14:00', '2022-11-27T15:00', '2022-11-27T16:00', '2022-11-27T17:00', '2022-11-27T18:00', '2022-11-27T19:00', '2022-11-27T20:00', '2022-11-27T21:00', '2022-11-27T22:00', '2022-11-27T23:00', '2022-11-28T00:00', '2022-11-28T01:00', '2022-11-28T02:00', '2022-11-28T03:00', '2022-11-28T04:00', '2022-11-28T05:00', '2022-11-28T06:00', '2022-11-28T07:00', '2022-11-28T08:00', '2022-11-28T09:00', '2022-11-28T10:00', '2022-11-28T11:00', '2022-11-28T12:00', '2022-11-28T13:00', '2022-11-28T14:00', '2022-11-28T15:00', '2022-11-28T16:00', '2022-11-28T17:00', '2022-11-28T18:00', '2022-11-28T19:00', '2022-11-28T20:00', '2022-11-28T21:00', '2022-11-28T22:00', '2022-11-28T23:00', '2022-11-29T00:00', '2022-11-29T01:00', '2022-11-29T02:00', '2022-11-29T03:00', '2022-11-29T04:00', '2022-11-29T05:00', '2022-11-29T06:00', '2022-11-29T07:00', '2022-11-29T08:00', '2022-11-29T09:00', '2022-11-29T10:00', '2022-11-29T11:00', '2022-11-29T12:00', '2022-11-29T13:00', '2022-11-29T14:00', '2022-11-29T15:00', '2022-11-29T16:00', '2022-11-29T17:00', '2022-11-29T18:00', '2022-11-29T19:00', '2022-11-29T20:00', '2022-11-29T21:00', '2022-11-29T22:00', '2022-11-29T23:00', '2022-11-30T00:00', '2022-11-30T01:00', '2022-11-30T02:00', '2022-11-30T03:00', 68 more items ], temperature_2m: [ 2.4, 2.3, 2.1, 1.9, 1.8, 1.7, 1.8, 1.7, 1.9, 2.3, 2.5, 2.6, 2.1, 1.1, 0.7, 0.5, 0.2, 0, 0.1, 0.1, -0.1, -0.1, -0.1, -0.1, -0.2, -0.2, -0, 0.1, 0, -0.1, -0.1, -0.1, 0.2, 1, 1.5, 2, 2.1, 2.1, 2, 1.7, 1.5, 1.3, 1.2, 0.9, 0.9, 0.9, 0.7, 0.6, 0.2, -0.1, -0.1, -0.1, -0.1, -0.2, -0.3, -0.4, -0.3, 0.4, 1.4, 1.8, 1.5, 1.4, 1.4, 0.9, 0.3, -0, -0.1, -0.2, -0.3, -0.3, -0.3, -0.5, -0.8, -0.9, -1, -1.1, -1.3, -0.3, -0.3, -0.4, -0.5, -0.6, -0.8, -0.8, -0.8, -0.8, -0.8, -0.9, -1,

As posted above, tried to extract one array from whole data asset

CodePudding user response:

from what I can see here, its a nested object and not an array, have you tried

data.hourly.temperature_2m

this should give you the temperature_2m data, and if that does not work out, try checking typeof data if its a string or an object

CodePudding user response:

const inquirer = require('inquirer');
const axios = require('axios');
const API = 'https://api.open-meteo.com/v1/forecast?latitude=';
let array0 = [];
 
const getWeather = async() => {
    const [lat, lon] = [51.77, 19.47];
    const WeatherURI = `${API}${lat}&longitude=${lon}&hourly=temperature_2m`;
    const response = await axios.get(WeatherURI);
    console.log(response.data);
    const {temp_c} = (temperature_2m);
    array0 = response.data(temperature_2m)
    console.log(array0)
}
 
inquirer
    .prompt([
        {
            type: 'list',
            message: 'Select a location',
            name: 'source',
            choices: ['Lodz', 'Bialystok'],
        },
    ])
 
    .then(async (answers) => {
        const weather = await getWeather();
        switch(answers.source) {
            case 'Temperature in Lodz':
                console.log(weather[temp_c])
            break;
            default:
            console.error('Invalid Option!')
            break;
        }
    })
    .catch(err => {
       console.error(err);
    });  
  • Related