Creating a JSON object hashtable using jsHashtable: A step-by-step guide

I have a good understanding of how to create a hashtable from scratch using jshashtable. For instance:

<script type="text/javascript" src="jshashtable.js"></script>
<script type="text/javascript">
    var typesHash = new Hashtable();

    typesHash.put("A string", "string");
    typesHash.put(1, "number");

    var o = {};
    typesHash.put(o, "object");

    alert( typesHash.get(o) ); // "object"
</script>

However, I am unsure about how to apply this method to my current project. I have over 1000 JSON objects similar to the following:

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

My goal is to create a loop that adds all these objects to a hash table. I want to be able to search for the unique value of "form_key" and retrieve the corresponding value of "data". I understand that this process will involve using JSON.parse() in JavaScript.

For example, running alert(basicPatientName) should output "Miranda Jones".

Answer №1

var info = [{"form_key":"basicUserInfo","data":"John Doe","cid":2,"pid":1,"no":"0"},
             {"form_key":"userGender","data":"1","cid":4,"pid":1,"no":"0"}];

To retrieve the data, simply iterate through and assign the key and value accordingly.

var detailsHash = new Hashtable();

for (var j = 0; j < info.length; j++) {
    detailsHash.put(info[j].form_key, info[j].data);
}

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

Ensure the camera flag remains set in three.js

I am currently experimenting with the WebGLRenderer in three.js and I am looking for a way to keep the camera in motion without resetting, similar to how it is shown in this video at the 4:00 minute mark. You can view the video here: https://www.youtube.c ...

Concealing a DIV element when the value is not applicable

I'm currently working on a website for a coffee shop product. On this site, each product has a coffee strength indicator that is determined by the database. The strength can be categorized as Strong, Medium, Weak, or n/a (for non-coffee products). If ...

Employ Angular within the parameters of a JavaScript function call

My goal is to incorporate Google Maps into my webpage for various locations (lieux). Within the HTML page, I have the necessary function set up for Google Maps. <script> function initialize(long,lat) { var mapCanvas = document.getElementById( ...

What is the best way to fetch all the orders from my product schema using response.send?

This is my custom Product schema const productSchema = new mongoose.Schema({ title: { type: String, required: [true, "Title is required"] }, details: { type: String, required: [true, "Details are r ...

Discover the best methods for accessing all pages on a Facebook account

I attempted to retrieve a list of all Facebook pages, but encountered an error. The error message states: 'request is not defined.' Here is the code snippet: var url = 'https://graph.facebook.com/me/accounts'; var accessToken = req.u ...

What causes the timer to pause, and what steps can be taken to avoid it? (Using Javascript with Iframe)

On my website, I have a page where clients must view an advertisement for 20 seconds. The website is displayed in an iframe with a countdown timer above it. I've set it up so that the timer stops when the window loses focus to ensure the client is ac ...

Ways to conceal a div using jQuery when the video source is missing

If there is no source in the video, I want to hide the video_wrapper class <div class="video_wrapper" style="width: 100%; height: 100%; display: none;"> <video id="df-video" playsinline="" webkit-playsinline="" style="height: 100%; width: 100%; ...

"Customize your map pins on Google Maps by updating the marker location through a

I am trying to update the position of a map marker based on a dropdown selection. Upon changing the dropdown option, I retrieve the latitude and longitude values and want to set these coordinates for my current marker. Here is the code snippet: $("#locati ...

When interacting with the iframe in an Ionic3 app, it suddenly crashes

Greetings! I have integrated a flipping book URL inside an iframe: <ng-container> <iframe [src]="eUrl" id="flipping_book_iframe" frameborder="0" allowfullscreen="allowfullsc ...

Having issues with the Carousel feature in Bootstrap 5.3.1 while using Angular version 15

I'm currently in the process of setting up a carousel on my homepage. It seems like everything is in place, but there's something missing. The initial image, text, and arrows all display properly, but they are non-functional. I have correctly imp ...

Upload files via Ajax request is required

I am in the process of trying to upload a binary file to a server while avoiding a full page refresh when the server responds. I must admit, I am not well-versed in this area and I understand if my approach needs some adjustments. This is how I have appro ...

Is there a way to incorporate a fade-in effect when I trigger the expand function in this script?

I recently came across a jQuery plugin for expanding and collapsing content. I am interested in adding a fade-in effect to this plugin specifically when the EXPAND button is clicked. How can I accomplish this? $(document).ready(function () { var maxlines ...

Set the value of a hidden field and proceed to submit it in CodeIgniter

I seem to be struggling with a simple task of setting a hidden input value. Here is how my form is structured: <?php echo validation_errors(); ?> <?php echo form_open('purchasing'); ?> <h1>Purchasing Re ...

How to request permissions from a bot user in Discord.js version 14?

I need to verify my bot's permissions before it carries out a command. Previously, everything was functioning perfectly with this code: // Discord.js v13 if (interaction.guild.me.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES)) { interaction.re ...

Trouble with activating dropdown toggle feature in Bootstrap 5

I recently upgraded to Bootstrap 5 and now my code is not functioning properly. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria- controls="navbarCollaps ...

Zero values disappeared from the parameters in an Ajax request

When I send parameters for a WebMethod using Ajax, I receive an object. However, the parameters are losing zeros in the WebMethod. For example, if I send "00001234", on the backend the parameter is received as "1234". Here is the Ajax code that I am using ...

Steps to Decode JSON in C#

I have limited experience in c# programming, but I am well-versed in Object-Oriented Programming with PHP and Java. In my current c# code, I am working with a HTTP web response that returns a JSON object. While I have researched various articles and Micros ...

Dividing a string into an array and displaying it in a table using Laravel

Retrieving a string from the database and using the explode function to split the values. Below is the code snippet: $data = DoctorRegistration::select('products') ->where('doctorid','=',$doctorid) ->get(); ...

The AJAX data did not successfully render the jQuery template

When I manually input the JSON string, the code below works perfectly. However, when I try to pass the JSON string returned from an AJAX call to render the template, it does not function as expected. Can someone assist me in identifying the issue? functio ...

Utilizing Alamofire to Parse JSON Responses

Upon receiving a JSON response in Postman, I encountered the following: { "errorcode": "0", "errorstr": "Success", "result": { "top": [ "id": 826133 "deal_detail": "<p><strong>Main link & ...