What is the best way to extract a specific value from a JavaScript function?

I've got a simple piece of code that finds the highest number in an array.

const getMax = (data) => Object.entries(data).reduce((max, item) => max[1] > item[1] ? max : item);

var myData1 = { a: 1, b:2, c: 3};
var myData2 = { a: 4, b:2, c: 3};

console.log(getMax(myData1));
console.log(getMax(myData2));

The result returned:

[ 'c', 3 ]
[ 'a', 4 ]

This function is crucial for various tasks. How can I specifically print only the first calculated value ('c' or 'a') and then specifically print the output of the second value (3 or 4)?

Appreciate any help on this. Thanks.

Answer №1

When a function is executed, it can only produce a single output value, whether that value is straightforward or intricate. However, you have the option to store the more complex result from the function and then access its individual components:

const getMax = (data) => Object.entries(data).reduce((max, item) => max[1] > item[1] ? max : item);

var myData1 = { a: 1, b:2, c: 3};
var myData2 = { a: 4, b:2, c: 3};
var result1 = getMax(myData1);
var result2 = getMax(myData1);
var simple1_0 = result1[0];
var simple1_1 = result1[1];
var simple2_0 = result2[0];
var simple2_1 = result2[1];
console.log(simple1_0);
console.log(simple1_1);
console.log(simple2_0);
console.log(simple2_1);

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

Autocomplete like Google with arrow key functionality

I have developed a basic search engine that retrieves data from a MySQL database using the PHP "LIKE" function (code provided below). Everything is functioning correctly, but I would like to enhance it so that users can navigate search results with arrow k ...

Determining the current element's index as I scroll - using Jquery

If I have a series of divs with the class name 'class' that are fixed height and enable auto-scrolling, how can I determine the index of the current 'class' div I am scrolling in? Here is an example: var currentIndex = -1; $(window).sc ...

What are some recommended methods in Angular for developing reusable panels with both controller and view templates?

I am still getting acquainted with angularjs, so there might be something I'm overlooking, but I'm struggling to find an efficient way to create reusable views that can be instantiated within a parent view. My specific scenario involves a web ap ...

Center the text vertically within a card using Vuetify styling

I am seeking a way to vertically align text within a card using Vuetify or traditional CSS in a Vue project. Here is my code: <template> <div> <v-container class="my-5"> <v-row justify="space-between"> <v- ...

Calculating the ReLU derivative using NumPy

import numpy as np def relu(z): return np.maximum(0,z) def d_relu(z): z[z>0]=1 z[z<=0]=0 return z x=np.array([5,1,-4,0]) y=relu(x) z=d_relu(y) print("y = {}".format(y)) print("z = {}".format(z)) The code shown above displays: y = ...

What is the best way to implement filter functionality for individual columns in an Angular material table using ngFor?

I am using ngFor to populate my column names and corresponding data in Angular. How can I implement a separate filter row for each column in an Angular Material table? This filter row should appear below the header row, which displays the different column ...

Dynamic jQuery slideshow with unique starting point

My photo collection is displayed in a list format like this: <ul class="slideshow"> <li><img src="images/slideshow/slide0.jpg" alt="" /></li> <li><img src="images/slideshow/slide1.jpg" alt="" /></li> & ...

Unraveling the Mysteries of Linguistic Evolution

I am curious about how to retrieve data from a map. I have three buttons on a JSP page: Register, Update, and Delete. The JSP files I am working with are First.jsp and Second.jsp. I have included First.jsp within Second.jsp using aaa. The buttons are l ...

The conversation reappearing prematurely before a response is chosen

Incorporating a dialog box that prompts the user with a question is crucial in this function's design. The code snippet below illustrates how it operates: function confirmBox2(action) { var message = ""; if (action == "Quit") { mess ...

Contrasting .queue() with jquery.queue()

Can someone clarify the distinction between .queue() with .dequeue() and $.queue() OR jquery.queue()? If they serve the same purpose, why did jQuery provide them in two separate documentations? Could someone provide examples to illustrate their difference ...

Difficulty capturing emitted events from child components in Vue.js2

Currently, I'm working on developing a Bootstrap tabs component using Vuejs. The tabs component is divided into two parts - the parent tabs-list component that contains multiple tab-list-item components. Take a look at the code for both these componen ...

Adding a command to open a new browser tab using JavaScript code can easily be accomplished by following these steps. By using Terminal, you can quickly and efficiently implement this feature

I'm new to coding and trying to create a terminal simulation. You can check out my code here: https://codepen.io/isdampe/pen/YpgOYr. Command: var coreCmds = { "clear": clear }; Answer (to clear the screen): function clear(argv, argc ...

How can I show information on the same page as a link by simply clicking on it?

My goal is to create a functionality where clicking on a link will display specific information. Currently, all the links and their corresponding information are displayed at once. I want to change this so that the links are displayed first, and the inform ...

What is the best way to showcase the information stored in Firestore documents on HTML elements?

Currently in the process of designing a website to extract data from my firestore collection and exhibit each document alongside its corresponding fields. Below is the code snippet: <html> <!DOCTYPE html> <html lang="en"> <head> ...

Obtaining various values for checkboxes using dynamic data in a React application

Retrieve all checkbox values dynamically import * as React from "react"; import Checkbox from "@mui/material/Checkbox"; import FormControlLabel from "@mui/material/FormControlLabel"; import axios from "axios"; expor ...

Maintaining the state of a React component across page refreshes, with a preference for storing the

As a newcomer to React, I am utilizing localstorage in my React app to store certain data that is needed for page refreshes along with the useEffect() hook. My only concern is that I wish to find a way to conceal this particular data, as it seems I cannot ...

switch out asterisk on innerhtml using javascript

Is there a way to replace the asterisks with a blank ("") in the innerHTML using JavaScript? I've attempted this method: document.getElementById("lab").innerHTML = document.getElementById("lab").innerHTML.replace(/&#42;/g, ''); I also ...

Why is the $match function in Mongoose not functioning properly with an array of ObjectIds in Node.js

Greetings! I've been trying to match an array of objectIds in Mongoose using Node.js, and although I achieved the desired result in the Mongo shell, I'm facing difficulties when implementing the same in my code. Here's a snippet from my col ...

Substituting a JavaScript function with a UserStyle solution

I am currently working on customizing a UserStyle for Instapaper. Since the original UserStyle was created, Instapaper has implemented several JavaScript functions in their header that control page width and font styles. Below are the functions: ...

How can you extract elements from a JSON array into separate variables based on a specific property value within each element?

In the following JSON array, each item has a category property that determines its grouping. I need to split this array into separate JSON arrays based on the category property of each item. The goal is to extract all items with the category set to person ...