What is the best way to form a Hashtable in JavaScript using a JSON object where the value serves as the key?

Forgive me if this seems unclear, but I am trying to locate specific values within each JSON object and retrieve another value based on it. I have been advised to consider using a Hash Table for this purpose, however, I am unsure of how to proceed. For instance:

{
    "form_key" : "basicPatientName",
    "data" : "Miranda Jones",
    "cid" : 2,
    "pid" : 1,
    "no" : "0"
}

I aim to search for basicPatientName and extract Miranda Jones, or search for basicPatientgender and extract 1.
I have utilized a library named DefiantJS which allows me to iterate through my JSON easily and accomplish what I need. However, I have been warned about the excessive number of iterations involved if I were to perform this task more than 1000 times in the same program.

Answer №1

Here is a code snippet that you can use:

sampleData = [{
    "form_key" : "basicPatientName",
    "data" : "John Doe",
    "cid" : 2,
    "pid" : 1,
    "no" : "0"
},
{
    "form_key" : "basicPatientAge",
    "data" : "25",
    "cid" : 4,
    "pid" : 1,
    "no" : "0"
}
];

var info = {};
sampleData.forEach(function (record) {
    info[record.form_key] = record.data;
});

// display information
console.log(info);

// Example of accessing data:
var name = info.basicPatientName; //  = John Doe

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

Is it possible for Web Service and MySQL to function on the iOs Simulator?

I am currently working on developing an app using XCode4. I have reached a point in my development process where I need to utilize web services to connect to a MySQL database for both retrieving existing user information and adding new account details. My ...

Shifting the pagination outside of the slider in ReactJS and SwiperJS seems to be tricky. Despite trying to adjust the margins and padding, the pagination

I've been struggling with this issue for a while now. I need to move the pagination outside of the slider, but using padding or margin doesn't seem to work. It always remains inside the slider, and if I try to position it outside, it gets hidden. ...

Web API OData failing to retrieve accurate metadata

I am currently utilizing OData v4 in conjunction with Web API to interact with my AngularJS web application. Specifically, I am attempting to showcase my data using Kendo UI Grid. The issue I'm encountering is that the metadata returned by my Web AP ...

The counterpart of the RxJS setTimeout operator

Looking for a RxJS operator alternative to set/clearTimeout in these circumstances: this.mouseEnterSubscription = this.mouseEnterStream .subscribe(() => { this.timeout = setTimeout(() => { void this.playVideo(); }, 500) }); this.mo ...

organizing arrays with the structure name:url

I have a list of string URLs like "http://dom/image1.jpg","http://dom/image2.jpg" that I obtained from an API which returns only the links. However, the plugin I am using requires the array to be in a specific format: {image:"http://dom/image1.jpg"},{imag ...

Using async and await inside a setTimeout function within a forEach loop

Currently, I am facing a challenge with scheduling background jobs in Node.js. I need each job to run only after the previous one has finished. Below is the code snippet inside a function: jobArray.forEach((item, i) => { setTimeout(async () => { ...

This element is not compatible for use as a JSX component

I have created a React component which looks like this import React from 'react'; import { ECOTileSummary } from './ECOTileSummary'; import { TileSummary } from './TileSummary'; interface SuperTileSummaryProps { date?: s ...

Tips for avoiding the babel/cli accessibility issue during Google Cloud deployment

I've encountered an issue while attempting to deploy my modularized Node.js Google cloud function to Google Cloud Platform. The function works smoothly on my local machine, but I'm getting an error during deployment that reads: ERROR: (gcloud.fu ...

Guide on organizing an array of objects by the sub-part of their property values

Is there a way to order the elements in the given array based on a custom criteria related to the last 3 letters of 'prop2' in descending order? The specific order should be 'ABR', 'FDE', 'ZFR'. Another array needs t ...

A guide on utilizing express.js to retrieve and display the output of a mySql query

I am facing an issue with getting the results of my simple SELECT command into the index.js file. I want to store all the records in an array, but when I try to do so, I always end up with undefined values. Interestingly, if I print the results in the data ...

The addition of meshes in real-time leads to a decrease in control responsiveness

I'm currently working on creating a 3D map using Three.js. My process involves building the geometry and texture with images of size 256x256 pixels, then constructing a THREE.Mesh to add to the scene upon completion. However, I've noticed that ...

Adding Data to a Database Using Ajax with PHP

I am trying to use Ajax to insert data into a database, but I keep encountering this error: Uncaught ReferenceError: insertData is not defined at HTMLInputElement.onclick (Modal.php:105) Error screenshot: https://i.sstatic.net/AmXka.jpg The HTML and ...

What could be causing my chessboard to not wrap around properly with flexbox?

I have been working on creating an 8x8 chessboard by following this example: https://codepen.io/sammyslamma/pen/gJeOwY .flex-container { display: flex; flex-flow: row; flex-wrap: wrap; ...

Issues with displaying calendar items on the React timeline are only resolved after pressing F12 or resizing the

I am encountering a problem while trying to plot the items array in the timeline calendar. Interestingly, the groups array is working fine. The items array also functions properly when values are provided manually. However, the items only render after pre ...

What could be causing my web application to not reload upon opening?

My web app is in need of an update using JavaScript. Currently, it loads data from a website but for this demonstration purposes, "Hello World" fails to display. Although not the best code practice, it highlights the issues I am facing with more complex co ...

Using MongoDB to find a specific element in an array and then make updates

I have performed aggregation to calculate the total and the objects that contribute to the total. Now, I need to update the source table with the aggregated object ID for the elements involved in the aggregation, establishing a two-way relationship. col ...

"Customize your website with sleek Rails jQuery dropdown menus for a multitude

Just dipping my toes into the world of jquery, feeling a bit lost with it (and javascript). After scouring the internet for hours, I stumbled upon a pretty straightforward method to create a dropdown menu. It does the job, but unfortunately, it only works ...

The inclusion of HttpClient is causing issues with the functionality of my component

Currently, I am facing an issue with my Angular service called ConnexionService. The problem arises when I try to incorporate CSV files into this service using HttpClient. Strangely, the component associated with this service fails to display once HttpClie ...

Error message "Unable to load / invalid sound with zero-length duration reported on Chrome and Firefox."

Encountered an issue where / invalid sound zero-length duration was reported in Chrome and Firefox. However, the error only appeared in IE10. Here is the code snippet: foo = soundManager.createSound({ id: 'bar', url: '../music/ ...

Is it really impossible to post complex JSON parameters with Restangular?

Is it possible to send a complex JSON object to a PUT route using Restangular? Restangular.one('model3ds', model.uuid).put( api_key: "blabla" model3d: { is_public: true } ) However, the data sent by Restangular appears as: ...