JavaScript: Grab a single numerical value from a key-value array and save it to a variable

After executing an InfluxDB query, I receive a single result row in the following format:

{"result":[[{"result":"_result","table":0,"_start":"2022-01-28T09:00:12.676771291Z","_stop":"2022-01-29T09:00:12.676771291Z","_measurement":"Strom.Heizung.Energie_in_der_letzten_Stunde","_value":14.683000000000117,"_time":"2022-01-29T09:00:12.676771291Z","ts":1643446812676}]],"ts":1643446812684,"error": null}

I'm looking to extract and store the value of the _value key into a variable, for example, value. In this instance, the value is 14.683000000000117.

I attempted to achieve this using the map function:

value = query.result[0].map(elm => (elm._value));

However, the value is returned within brackets like [14.683000000000117]. How can I obtain just the number in my value variable?

Thank you,

Frank

Answer №1

When using the <code>map() function, keep in mind that it always returns an array, even if it only contains one element. To get the first element of this returned array, simply add [0] at the end:

To extract the desired value from the result array, you can use the following code snippet: <code>value = query.result[0].map(elm => (elm._value))[0];

If you want to learn more about accessing elements in arrays, check out the "Accessing Array Elements" section on this link:

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

Enhancing next-auth and i18n functionality with middleware in the latest version of next.js (13) using the app

Currently, I have successfully set up next-auth in my next.js 13 project with app router, and it is functioning as expected. My next step is to incorporate internationalization into my app following the guidelines provided in the next.js documentation. How ...

What is the reason for the identical nature of these two arrays?

Looking to make a copy of my list and then sort it without altering the original in Python. Below is my code snippet: def swapBySoting(arr): newArr = arr newArr.sort() swap = 0 for i in range(len(arr)): if arr[i] != newArr[i]: ...

Command not found: execute - firebase deploy

After deploying my Firebase function project, I encountered an error that said "missing script:build." I have tried to fix it on my own but have been unsuccessful. I would greatly appreciate any assistance Here is a screenshot of the error https:// ...

Changing a 1D byte image array to a 2D byte array using Java

Working with a 1D byte array for a buffered image, I encountered the need to convert it to a 2D byte array. Below is the code I wrote for this purpose: File file = new File("/home/tushar/temp.jpg"); try { input_bf = ImageIO.read(file); wid ...

Choosing custom element children once they're connected to the DOM: A guide

My goal is to retrieve the value of transaction-name__inputbox when the user clicks on the transaction-add__button. The function transactionAddHandler is triggered upon clicking the button. However, my attempt to select this element using document.querySe ...

Obtain the name of the client's computer within a web-based application

I am working on a Java web application that requires the computer name of clients connecting to it. Initially, I attempted to retrieve this information using JavaScript and store it in a hidden form field. However, I discovered that JavaScript does not hav ...

The 'name' property of Axios and Vuex is not defined and cannot be read

When making a call to axios in my mounted function to retrieve profile data, I send the payload to the 'set_account' Vuex store upon success. To access this data, I utilize MapGetters (currentAccount) in computed properties. However, when tryin ...

Exploring the capabilities of JW Player 6 for seeking and pausing video

Is there a way to make JW Player 6 seek to a specific point and pause without pausing after each seek request, maintaining the ability to seek continuously during playback? The current solution provided pauses the player after every seek request, which is ...

Reactjs and Redux encounter an Unhandled Rejection with the error message stating "TypeError: Cannot read property 'data' of undefined"

Encountering an error while implementing authentication with react + redux. When attempting to register a user in the redux actions using async / await, I consistently receive this error within the catch block. Here is the snippet of the actions code: imp ...

Receiving an array of data from a SOAP service using PHP

Hi there, I'm a beginner in PHP and I'm slowly learning the basics. I recently encountered an issue where I am unable to receive an Array from a SOAP server. Here's a snippet of my code: class cUSER { public $LOGIN; public $ ...

When I clicked on the event in Javascript, the result was not what I expected

I am currently working on a web project centered around cooking recipes. In order for users to add ingredients to their recipes, they must input them one by one into a dynamic list that I am attempting to code using jQuery (AJAX). My issue arises when a u ...

Revise a button using JavaScript

Hey there, I could really use some assistance. Currently, I am learning Javascript as a part of a project I am working on and I seem to have run into a bit of an issue with updating the display status of a button on my webpage. I am trying to set up a ...

Is it possible to import multiple versions of a JavaScript file using HTML and JavaScript?

After extensive research, I am starting to think that using multiple versions of the same JavaScript library on a single page is not possible (without utilizing jquery Can I use multiple versions of jQuery on the same page?, which I am not). However, I wan ...

Ways to initialize data for an HTML snippet with its own controller?

When developing an Angular application, I commonly employ resolves to fetch necessary data for a controller handling a template for a specific route. Currently, I am facing a new scenario in which there are HTML fragments embedded within a route. How can ...

Utilizing React-Spring's useSprings specifically for the active button within an array

I'm currently working on animating the position change of the active box. When the user clicks on a box, I want that box to smoothly transition with a margin-top adjustment while keeping the other boxes at 0px. As the user switches between boxes, the ...

What is the best way to select a cell within the same row using jQuery?

I've successfully implemented a table with a single input field and an AJAX script that triggers when the input field value is changed. Everything is functioning as expected. However, I now face the challenge of adding a dynamic date insertion feature ...

The unzipper will disregard any empty directories within the file

I am a Japanese Web Developer. Unfortunately, I struggle with English. https://www.npmjs.com/package/unzipper This library is currently in use by me. Below you will find my code snippet: // unzip module import fs from 'fs-extra' import unzip ...

Add an element to an array using the `push` method in JavaScript

I am currently studying the code for creating a canvas circle motion trail effect. I am a bit puzzled by the push(x:Pos,y:Pos) within thestoreLastPosition() function in the sample code provided below: ctx = canvas.getContext('2d'); var xPos = - ...

Oops! Looks like there's an issue with the NextJS Custom Carousel bug. The error message reads: ReferenceError: document is not defined and is unable to read properties of undefined (

I have developed a unique carousel component to showcase text testimonials from scratch. The custom carousel utilizes a specialized script, carouselFunction.js, for navigational functionality to cycle through each testimonial. Here is an image of the carou ...

How can one go about designing a function in C that retrieves the size of an array?

Is there a way to determine the size of an array in C using a function? Keep in mind that functions and arrays have different scopes. Here is an example: int func(int arr[]); int main(){ int arr[5]; } int arr_length(int arr[]){ int length = sizeo ...