Generate a JavaScript variable in real-time and leverage it to retrieve the corresponding JSON response

After receiving a JSON response from an API as depicted in the image below: https://i.sstatic.net/5eq43.png

I encountered an issue where the same column_code is used in both the variable_meta section and the data part of the response. This column code acts as the variable name for fetching time-series data, leading to confusion on how to obtain the data in this scenario.

I attempted employing the window method but faced difficulties with its implementation. In the code snippet provided, my objective is to dynamically replace the utilization of FRWRTM_AirT_2_2_A with a programmatically generated variable.

if( x[i].FRWRTM_AirT_2_2_A !=-9999) {
      d = new Date(x[i].date_UTC)
      y.push([d, x[i].FRWRTM_AirT_2_2_A])
      //console.log(d, x[i].FRWRTM_AirT_2_2_A)
    }

Edit : Including the JSON response requested by some:

{
    "meta_data": {
        "station_meta": [
            {
                "city": "xxx",
                "station": "yyy",
                "lat": 58.992778,
                "lon": 15.844722,
                "height": 277,
                "landcover": "Artificial Surfaces"
            }
        ],
        "variable_meta": [
            {
                "city": "xxx",
                "station": "yyy",
                "variable": "Air Temperature",
                "variable_logger_name": "CS215_AirTemperature_degC_Avg",
                "height": 2,
                "aggregation": "A",
                "level": 2,
                "unit": "ºC",
                "column_code": "FRWRTM_AirT_2_2_A"
            },
            {
                "city": xxx",
                "station": "yyy",
                "variable": "Air Temperature",
                "variable_logger_name": "CS215_AirTemperature_degC_Min",
                "height": 2,
                "aggregation": "I",
                "level": 2,
                "unit": "ºC",
                "column_code": "FRWRTM_AirT_2_2_I"
            },
            {
                "city": "xxx",
                "station": "yyy",
                "varible": "Air Temperature",
                "variable_logger_name": "CS215_AirTemperature_degC_Max",
                "height": 2,
                "aggregation": "X", 
                "level": 2,
                "unit": "ºC",
                "column_code": "FRWRTM_AirT_2_2_X"
            }         
        ]
    },
    "data": [
        {
            "date_UTC": "2018-05-14 11:00:01",
            &fracr;FRWRTM_AirT_2_2_A": -9999,
            "FRWRTM_AirT_2_2_I": -9999,
            "FRWRTM_AirT_2_2_X": -9999
        },
        {   
            "date_UTC"::"2018-05-14 12:00:01",,
            "FRWRTM_AirT_2_2_A": -9999
            "FRWRTM_AirT_2_2_I"": -9999,
            "FWRTM_Airt_2_2_x".: -9999     
        },   ]
}

Answer №1

Is this the data you are seeking?

let response = DATA_FROM_FETCH_REQUEST; // { meta_data: ..., data: ... }
let column_name = response.meta_data.variable_meta[0].column_name;
let column_value = response.data[0][column_name];

if(x[i][column_value] !=-9999) {
  d = new Date(x[i].date_UTC)
  y.push([d, x[i].FRWRTM_AirT_2_2_A])
  //console.log(d, x[i].FRWRTM_AirT_2_2_A)
}

Answer №2

To retrieve an array containing all the column_code values from the 3 elements within the variable_meta array, follow these steps:

const columnValues = yourData.meta_data.variable_meta.map(vm => vm.column_code);

Next, based on how you want to structure your data, loop through your data and the columnValues array as shown below:

const filteredFields = columnValues.map(cv => 
  yourData.data.filter(d => 
    (d[cv] || d[cv] === 0) && d[cv] != -9999
  ).map(d => 
    [new Date(d.date_UTC), d[cv]]
  )
);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with ReactJS: onChange event does not function properly when value is changed using jQuery

This is the reactjs code I am working with: <input name="date" id="date" value={this.state.listManage.date} onChange={this.handleForm} type="text" /> When I manually type in the input field, the onChange function works ...

Is it possible for me to verify the login status of an Auth0 user within my custom NextJS _app.js file?

Currently working on a NextJS application with nextjs-auth0 for authentication, which is completely new to me. I followed the documentation's suggestion and wrapped my _app.js with UserProvider, also using getInitialProps to set a global online/offlin ...

Choose a specific <div> element from an external page using Ajax/PHP

I'm encountering a small issue. I am currently utilizing Ajax to dynamically load content from another page into a <div> element after detecting a change in a <select>. However, my specific requirement is to only load a particular <div& ...

Emberjs Troubleshooting: Issue with Sending Many-To-Many JSON Parameters

My current versions are ember-cli 1.13.8 and ember-data 1.13.11, using ActiveModelAdapter. I am working with two objects: posts and users. On an individual post page, I have implemented an action that allows users to watch/follow the post. //post model j ...

How about passing some vue props data over?

I am trying to assign different colors based on the percentage of each of the 5 batteries. However, the issue I'm facing is that the color spread is not specific to each individual battery. I suspect this issue arises because the bmss data is being r ...

What is the best way to merge two approaches for tallying items within each category?

I have an Angular 8 application that includes two methods for displaying the number of items in each category. These items are retrieved from the back-end and are categorized as follows: <mat-tab> <ng-template mat-tab-label> ...

Discover the best way to retrieve XML information from various sources specifically designed for Windows gadgets using JavaScript

I have previously visited this site and have not been able to locate a solution that I believe will be suitable for my Windows 7 desktop gadget. In essence, I am seeking a method to retrieve XML data from weather.gov using Javascript (or any other means t ...

How do I retrieve the value of a class that is repeated multiple times?

In the HTML code I am working with, there are values structured like this: <div class="item" onClick="getcoordinates()"> <div class="coordinate"> 0.1, 0.3 </div> </div> <div class="item" onClick="getcoordinates() ...

Engaging with the crossrider sidepanel extension

When it comes to the crossrider sidepanel, I prefer using an iframe over js-injected html to avoid interference with the rest of the page. However, I am struggling to establish any interaction between my browser extension and the iframe. I believe adding ...

Making a dropdown menu spin using Jquery and Javascript

I am in need of a unique solution for a dropdown menu that appears rotated 90 degrees anticlockwise. The goal is to have the dropdown select "button" text displayed vertically, with the options sliding out in a similarly rotated, sideways manner featuring ...

"Automate the process of clicking on a specific part of a div element

I'm trying to extract data from this specific website: I've written the code to reach the simulation page, but I encounter an issue with a particular link. This dynamic link is causing me trouble when trying to access it. Clicking on that link ...

Establish a React component to observe socket.io

I am currently looking for the best way to connect my React component with socket.io in order to listen to events. My current approach involves including the following HTML code: <script src="socket.io/socket.io.js"></script> and then initial ...

Ensuring the existence of a MySQL database prior to executing a Node.js application

I am currently working on a Node.js/Express application that communicates with a MySQL server using Sequelize. I want to make sure that a particular database is created before the app starts running when using npm start. I think I need to create a one-ti ...

Serialize Jackson without including any properties

My JSON structure currently looks like this: { "name": "LS", "stocks": [ { "x": 1500234365000, "y": 0.000000000000 }, { "x": 1500234427000, "y": 0.000015154000 }, { "x": 1500234 ...

Encountered an Angular HPM localhost error while attempting to proxy a request for /api/books from localhost:4200 to http://localhost:3333

After spending several hours attempting to resolve this issue, I am still unable to successfully make a post request due to an error. Initially, I used ng serve, but switched to npm start and the errors persist. Error: [HPM] Error occurred while attempti ...

What is the best way to accurately determine an element's location after scrolling to it?

I'm currently working on pinpointing the location of a specific element on a webpage once it has been scrolled to using JavaScript. Queries: How can I precisely determine the position of an element post-scroll? What are some common errors or extra st ...

Handling Exceptions in Node.js and Express

Having recently delved into the world of javascript and node, I've been working on creating an application using node.js and express. While I've implemented appropriate error callbacks in my code, there are instances where the node.js server abr ...

Subheaders that stay in place while scrolling through a table with a stationary header

My goal is to design a table with a fixed header that allows the body to scroll under the header. While this is a common design, I am facing the challenge of implementing sticky subheaders. These subheaders should scroll along with the table until they rea ...

Elements from Firebase failing to appear in Angular Grid

I'm struggling to populate items within this grid sourced from Firebase. I was able to make it work with a custom Service that returned a fixed array. I can confirm that the data is being received by the browser as I can log it out in JSON format lik ...

Determine the data type of a string without needing to convert it

Can you determine if a value is numeric without casting it? For example, how can you differentiate between 'abc' and '123' without converting them to a specific data type? While visually apparent that 'abc' is not numeric, t ...