Retrieve a JavaScript object based on a specific value

Looking at my object :

TeamMember = { 
  0 : {"ID": 100, "Name": "MNO", "Role": 2},
  1 : {"ID": 101, "Name": "PQR", "Role": 3},
  2 : {"ID": 103, "Name": "STU", "Role": 3}
}

I am trying to retrieve

TeamMember[1] : {"ID": 101, "Name": "PQR", "Role": 3}
and I currently have the value 101. How can I accomplish this?

Your help is greatly appreciated!

Answer №1

Your code structure might need some adjustments, but consider this alternative format:

Employee = {
  0 : {"ID": 100, "Name": "ABC", "Level": 2},
  1 : {"ID": 101, "Name": "EFG", "Level": 3},
  2 : {"ID": 103, "Name": "KLM", "Level": 3}
}

If you access Employee[1], it will return

{"ID": 101, "Name": "EFG", "Level": 3}

Refer to https://www.w3schools.com/js/js_json_objects.asp for additional insight.

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

Using JavaScript or TypeScript to locate the key and add the value to an array

My dilemma involves an object structured as follows: [{ Date: 01/11/2022, Questionnaire: [ {Title: 'Rating', Ans: '5' }, {Title: 'Comment', Ans: 'Awesome' } ] }, { Date: 01/11/2022, Questionnaire ...

Button for expanding Google Maps API v3

My Ionic app using Google Maps v3 API recently added a new button that expands the map, but I can't figure out how to remove it. Has anyone encountered this issue before? If you need a visual reference, here is a screenshot showing the button at the ...

In jqGrid's gridComplete event, we can use the getRowData method to retrieve the value of a

Seeking guidance on extracting variables from jqGrid getRowData method While iterating through rows, I simply want to retrieve the ID and Phrase column values into separate variables gridComplete: function () { var allRowsInGrid = $('#list'). ...

I'm looking for guidance on utilizing the NodeRT (Windows.Gaming.Input) module within an electron environment. Can anyone provide some

I'm having trouble importing the Gamepad class from a specific module, as I keep encountering the error message "Invalid arguments, no suitable constructor found." Here is the code snippet in question: const { Gamepad } = require('@nodert-win10-2 ...

What steps should be taken to gain access to the FormController if the form is contained within a directive?

Here is the code for my custom directive: restrict: 'E', scope: { }, templateUrl: 'directives/my.directive.html', link: function(scope) { // I want to be able to access "myForm" here (e.g., to setPristine(), etc.) scope.custom ...

Efficiently Managing $http Errors and Success in AngularJS

At the moment, my code snippet is set up to display an error message that disappears after a few seconds when an Error occurs. Similarly, it shows a success message that also disappears after a few seconds upon receiving a successful response. However, I n ...

Executing functions with directive controllers

Is there a simple way to call a function on a directive controller by accessing the instance using its id from the parent controller? <my-directive id="id1" /> var dirController = getDirectiveByID("id1"); dirController.someFunc(); If you have any ...

Handling every promise in an array simultaneously

I am facing a problem with my array inside Promise.all. When I try to execute a function for the last iteration of forEach loop, I notice that my count_answers variable is only being set for the last object. This can be seen in the log output; the count_an ...

What is the best way to limit a form to only allow 2 checkbox selections?

Seeking advice on implementing a form for a website giveaway featuring 3 prizes. Each participant should only be able to select 2 items from the list. I've already created a JavaScript-based form, but I'm concerned about its reliability since it ...

transferring information from Node.js/MongoDB to the front-end (invisible in the browser)

I am trying to retrieve data from a mongodb database and pass it to the front-end. The function I have written works in the console, where I can see an array containing elements. However, when I try to view it in the browser, it shows undefined. I am worki ...

Tips for assigning a class to an Angular accordion header when the panel is in the open state

I am currently utilizing the Angular UI Bootstrap accordion feature, which can be found here. <div ng-controller="AccordionDemoCtrl"> <div accordion close-others="oneAtATime"> <div accordion-group heading ...

The response you have received is delayed by one response

I seem to be facing an issue with my Node.js server where the response I receive is always delayed by one. The response I expect to get at the time of pressing the upload button is always received one step later. After starting the Node server, the initia ...

Book Roulette: Your Next Random Read

I created a code for a random quote generator and now I want to create something similar, but with images this time. I am looking to add a feature on my page where it can recommend a book by displaying its cover image when a button is clicked. For the pre ...

Utilizing a backup system to store environment variables within a configuration file

Currently, I am utilizing my environment variables by directly referencing process.env.NODE_ENV throughout my application. While this method works, it is becoming challenging to manage and keep track of. Therefore, I would like to consolidate all these var ...

Exploring the world of if/elseif/else in JavaScript with ANTLR

I have been struggling with a particular problem and despite my efforts, I haven't been able to find a solution. Any guidance in the right direction would be greatly appreciated. My current challenge involves parsing a JavaScript file using ANTLR in ...

managing information through the elimination of nested keys with node js / javascript

let json=[ { metadata: { tags: [] }, sys: { space: { sys: [Object] }, id: '4gSSbjCFEorYXqrgDIP2FA', type: 'Entry', }, fields: { richTextEditor: { 'fn-US': [Object] }, short: { 'as-ds': &a ...

Building a hierarchical tree structure using arrays and objects with Lodash Js

I am attempting to create a tree-like structure using Lodash for arrays and objects. I have two arrays, one for categories and the other for products, both with a common key. The goal is to organize them into a tree structure using string indexing. let ca ...

Error: Unable to access the `insertUsername` property as it is not defined

When I attempt to submit the login form created by the new.ejs file, instead of being redirected to the expected page, I am encountering an error message that reads: Cannot read property 'insertUsername' of undefined This same error message is ...

What is the best way to present retrieved JSON data from a database in Node.js without using the JSON format?

Here is the code snippet: var http=require("http"); var fs = require("fs"); var express = require("express"); var app = express(); var path = require("path"); var mysql = require('mysql'); var ejs = require("ejs") var bodyParser = require(&apos ...

The results obtained from using $http get are not the same as those obtained

While attempting to retrieve user reports using a User $resource, I encountered an unexpected object format in the returned result: {0: "n", 1: "a", 2: "m", 3: "e", 4: ",", 5: "e", 6: "m", 7: "a", 8: "i", 9: "l", 10: ",", 11: "p", 12: "a", 13: "r", 14: "t ...