A guide on accessing a dynamic object key in array.map()

How can I dynamically return an object key in array.map()?


Currently, I am retrieving the maximum value from an array using a specific object key with the following code:

Math.max.apply(Math, class.map(function (o) { return o.Students; }));

In this code snippet, 'class' represents an array and 'Students' is the key of each object within the array.


However, every time I need to find the maximum value, I have to write out that entire code block. To streamline this process, I decided to create a common method like so:

 getMaxValue(array: any[], obj: key) {
      return  Math.max.apply(Math, array.map(function (o) { 
               return o.key; // Here I intend to return student objects
               }));   
           }

With this method, I can simply pass in an array and the desired key (e.g., 'students') to retrieve the maximum value whenever needed:

 var maxOfStudents = getMaxValue(class, "Students");

My question now is how can I dynamically return the key from an object within array.map()? Is there a way to achieve this?

Answer №1

Implement the use of bracket notation to access a property using a string from an object.

const value = myObj['propertyName'];

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

My Angular2+ application is encountering errors with all components and modules displaying the message "Provider for Router not found."

After adding routing to my basic app through app.routing.ts, I encountered errors in all of my test files stating that no Router is provided. To resolve the errors, I found that I can add imports: [RouterTestingModule], but is there a way to globally impo ...

Mixing up jQuery's Deferred, jsDeferred, and the concept of deferring in coding can be a common source of confusion

I recently downloaded a library called jsdeferred in hopes of resolving some code-flow issues I've been facing. However, I'm finding the examples and documentation to be a bit unclear. In my quest for clarity, I also discovered that jQuery offers ...

Having trouble targeting a div with jQuery

Is it possible to target a specific div within an unordered list and list items? I'm having trouble with it. Here is the HTML code: <ul class="grid"> <div id='categoria' cat='web'></div> <li id=' ...

Display or conceal a div element depending on the value selected in Vue.js

I have a Vue.js dropdown and I would like to show or hide my div based on the selected value in the dropdown. The current code is only working for one ID, but I want it to work for all IDs. I want to toggle the visibility of my div based on the option&apos ...

Incorporating Vaadin components into an Angular2-seed project

Hi there, I've been encountering an issue while trying to integrate Vaadin elements into my Angular2 seed project. The Vaadin team has recommended that I upgrade the systemjs.config.js file by specifying the path names for Vaadin elements like this: ...

What causes content to overflow a flex container?

Hey there, I currently have the following structure: <div fxLayout="row" fxLayout.xs="column" fxLayoutAlign="space-around start" fxLayoutGap="2%" style="padding:2%;"> <div fxFlex style="line-height:2;"> <md-card> ...

I'm facing the challenge of trying to work on an Angular project without access to the node_modules folder, and unfortunately, I

Error report regarding npm resolution While resolving: [email protected] Found: @angular/[email protected] node_modules/@angular/common @angular/common@"^14.2.12" from the root project Could not resolve dependency: peer @angular/commo ...

Issue with wiring Grails command object when attempting to delete using Restangular

I've been working on developing a REST-style service using Grails. The code snippet I have is as follows... def delete(Question q){ def text = request.reader.text; def slurper = new JsonSlurper(); def result = slurper.parseText(text) ...

The option value in mat-autocomplete is not displaying correctly on IOS devices

When I click on the first option in the dropdown menu, it does not display the selected option in the field. However, when I select the second option, then the value of the first option appears, and when I choose the third option, the value of the second o ...

Synchronize Protractor with an Angular application embedded within an iframe on a non-Angular web platform

I'm having trouble accessing elements using methods like by.binding(). The project structure looks like this: There is a non-angular website | --> Inside an iframe | --> There is an angular app Here's a part of the code I'm ...

How to prevent unnecessary new instances from being created by the Inject() function in Angular

Can someone please clarify if the inject() function provides different instances of a service? I suspect this might be why my code is not functioning as expected. Let's examine the code snippet below: { path: 'recipes', comp ...

Bring back object categories when pressing the previous button on Firefox

When working with a form, I start off with an input element that has the "unfilled" class. As the user fills out the form, I use dynamic code to remove this class. After the form is submitted, there is a redirect to another page. If I click the "back" ...

A guide on setting up dual observables in Angular 2

Currently, I am implementing Observable in angular 2 with rxjs. As part of my demonstration, I have utilized fromEvent in a Plunker. Here is the link to my demo: https://plnkr.co/edit/zkgEcdn21CvIKoOycUOy?p=preview In this demo, I have included two input ...

Can you save data entered by a user into a text file using JavaScript or a similar technology in HTML, without the need to send it to a server?

Is there a way to create a site where user data is inputted into an input box or form, and then that information is stored in a .txt file on the user's C drive without uploading it to a server first? I've been experimenting with various forms an ...

`Count the number of rows needed to accommodate text line breaks within a div element`

Is there a method to determine the number of lines that text breaks into using the CSS property word-break: break-all? For example, if I have a div like this: <div>Sample text to check how many lines the text is broken into</div> And the corr ...

The Uib-Dropdown functionality is not functioning properly when placed within the body of an HTML view

After correctly installing the following dependencies: "ui-bootstrap": "0.12.2", "ngAnimate": "1.5.5", "AngularJs": "1.5.5 I encountered an issue with creating a dropdown menu in my HTML view. Despite no visible errors and successful implementati ...

The alertify.alert function encounters issues when used within the response of a mithril m.request

I'm currently working on a project utilizing mithril.js and also integrating alertify.js. I am encountering an issue when trying to alert some data in the response. Strangely, it doesn't work as expected. However, if I use the same alert function ...

There seems to be a problem with the data-binding functionality in the AngularJS code through

My issue arises when I attempt to update the default values in the textArea after clicking the button. The changes made to the text (firstName/middleName/lastName) fields do not reflect upon clicking the button. The code related to this problem is spread ...

passport-local-mongoose req.user is currently not defined

While implementing passport js with the passport local mongoose plugin, I am facing a specific issue. Account creation and logging in are functioning properly. However, once logged in, passport fails to recognize me as a logged-in user. The code snippets ...

ngIf is not refreshing my template

Within my Angular2 application, I have a Component that takes an array as input and utilizes a service to retrieve the Latitude and Longitude for each entry in the array. This involves making multiple asynchronous calls to the service using Promises. expo ...