Retrieve Data from an Object using a Different Value

It sounds silly, but I'm struggling with this.

I'm working with a basic JS object:

var objSyntax = [ {"a": "1"},
                  {"b":  "2" },
                  {"c":  "3"} ];

My goal is to retrieve the value associated with key 'a' when using theid.

Here's the code I attempted, but it didn't work as expected:

objSyntax[theid];

What am I missing here?

Answer №1

Consider updating your object to look like this:

let newObj = {"x": "10",
                  "y":  "20" ,
                  "z":  "30"};
newObj[theId];

Alternatively, you can loop through the array of objects provided:

let newObj = [ {"x": "10"},
                  {"y":  "20" },
                  {"z":  "30"} ];

Answer №2

To call the current setup, you would use the following method:

objSyntax[0][theId]

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

How can I trigger an event in Vue.js when a selection is made in a dropdown menu?

Here is an illustration fiddle: https://jsfiddle.net/40fxcuqd/ Initially, it shows the name "Carl" If I choose Carol, Clara, etc., an event will be triggered and data will be logged to the console. However, if I click on the dropdown and select "Carl" ...

Tips for refreshing tableview cells thanks to an array sourced from an NSObject class

Whenever I attempt to fetch data from the detail view controller, the master view controller fails to add a new cell. My goal is to update the table view in the master view controller based on the array in the detail view-controller. Additionally, the canc ...

Setting up a project with Angular 2 and NodeJS

Hello there, I have some inquiries about organizing the project structure of a MEAN application with Angular2. Initially, I followed the introductory guide on angular.io to create a basic Angular2 app. Now, I am attempting to incorporate this app into a N ...

Creating a task list without using JavaScript in the web browser and without relying on a database

Looking for some guidance on building a todo app for a job interview where JavaScript is disabled in the browser and no database can be used. Any JavaScript needs to be handled on the server side. I have some basic knowledge of node/express and serving H ...

What is the best approach to retrieve a username from a SQL database using AJAX within a Flask application

I have been attempting to retrieve the username column information from SQL using ajax, but only the username of the first ID is being returned. However, I need all usernames to be fetched. Below is the model: class users(db.Model): id=db.Column(db.I ...

Encountering an issue with eslint-loader in a new VueJS project: TypeError - eslint.CLIEngine is not recognized as

Embarking on a fresh VueJS venture with WebStorm. A brand new VueJS project has been established, NPM upgraded, Vuetify added, and upon server initiation, an error pops up: ERROR Failed to compile with 1 errors ...

Unexpected behavior with if statements in jQuery

Having recently started using jQuery, I am embarking on the creation of a survey. Each question within the survey resides in its own div and is unveiled upon clicking the "Next" button. Currently, all divs are hidden except for the current one. My approach ...

How to Choose Between Landscape and Portrait Printing Modes in Firefox and Internet Explorer 8

Currently, I am using the latest version of FireFox and IE8. In order to change the printing orientation, I utilized the following code in my CSS file: @page { size: portrait; } You can find more information about the @page property here. Although it i ...

Next.js app experiencing issues with Chakra UI not transitioning to dark mode

After attempting to incorporate Chakra UI into my Next.js application, I carefully followed every step outlined in their documentation: Despite setting the initialColorMode to "dark" for the ColorModeScript prop, it seems that the dark mode is not being a ...

Error Encountered when Attempting to Retry AI SDK on Vercel's

I've been implementing code from the official AI SDK website but unfortunately, the code is not functioning properly. The error message I'm encountering is: RetryError [AI_RetryError]: Failed after 3 attempts. Last error: Failed to process error ...

The list retrieved via ajax appears to be void of any elements, despite the array size being accurately reflected

I am attempting to pass a List from the controller to an ajax function in order to display images in a modal carousel. The process begins when I click on an image, which triggers the ajax function to send a value. This value is then used to call a C# metho ...

Determine the vertices of the near and far planes by utilizing the THREE.Frustum method

Seeking assistance with the THREE.Frustum object. The Issue: I am trying to calculate near and far plane vertices, referencing these tutorials: Tutorial 1 Tutorial 2 I have implemented a function based on the explained procedure for obtaining top-left ...

I have an npm package that relies on X (let's say material-ui). What is the best way to prevent users from having to install

Hey everyone, I recently released an npm package called X that relies on material-ui as a dependency. While many users of X already have material-ui installed, there are some who do not. How can I ensure that those who have material-ui installed continue t ...

Executing a PowerShell script with a parameter of a string array

I'm currently developing a PowerShell plugin for a monitoring tool. This monitoring tool executes the PowerShell script from the command line through cmd. One of the input parameters is supposed to be a string array, but it seems like cmd is having ...

Empty input fields in Javascript calculations will result in a NaN output

I need to perform a calculation using values entered into form fields and JavaScript. The formula I'll be using is as follows: totalEarnings = income1 + income2 * 0.7 + income3 / 48 + (income4 * 0.7) / 48; The variables income1, income2, income3, an ...

Inspecting CSS values through Javascript is an important aspect of front

How can I create a JavaScript function that checks my CSS code and changes the background color to #29fd53 if it is currently #222327? Likewise, how do I make it switch back to #222327 if the current background color is #29fd53? This is my JavaScript code ...

Adding a feature to a React project

After importing the following code snippet into a test file: https://i.sstatic.net/i44pI.png Everything seems to be working fine. However, issues arise when trying to import it into another file—here is what I'm using: https://i.sstatic.net/dsBQu ...

Creating Comet applications without the need for IFrames

Currently embarking on my journey to develop an AJAX application with server side push. My choice of tools includes Grizzly Comet on Glassfish V2. While exploring sample applications, I've noticed that most utilize IFrames for content updates on the c ...

Organize the array of objects

Within my React state, I aim to rearrange a set of 3 objects by always placing the selected one in the middle while maintaining ascending order for the others. Currently, I utilize an order property within each object as a way to keep track of the sequenc ...

User form not triggering post requests

I have a unique react blog application embedded with a form for submitting intriguing blog posts. The setup includes a server, routes, model, and controllers for fetch requests. Surprisingly, everything functions impeccably when tested on Postman. However, ...