What is the process of dynamically creating a Javascript object?

I have been experimenting with creating the following custom array:

var deal_info = {
    "1": {
        "deal": {
            "deal_id": "1",
            "qty": "1",
            "option_price_sale": "7900",
            "price_ship": "2500",
            "ship_price_type": "1"
        }
    },
    "2": {
        "deal": {
            "deal_id": "2",
            "qty": "1",
            "option_price_sale": "3880",
            "price_ship": "2500",
            "ship_price_type": "1"
        }
    }
};

In the initial stage, my code started with:

var deal_info = {};

I am now wondering how I can dynamically continue building this array.

Your assistance is greatly appreciated!

Answer №1

This is not a regular array, but rather an object with two properties.

"1" : {                 
            "deal" : {
                "deal_id" : "1",                        
                "qty" : "1",
                "option_price_sale" : "7900",
                "price_ship" : "2500",
                "ship_price_type" : "1"                     
            }
        }

Additionally,

"2" : {
            "deal" : {
                "deal_id" : "2",                        
                "qty" : "1",
                "option_price_sale" : "3880",
                "price_ship" : "2500",
                "ship_price_type" : "1"                     
            }
        }

To convert it into an array, it can be written as follows:

var deal_info = [
        {                 
            "deal" : {
                "deal_id" : "1",                        
                "qty" : "1",
                "option_price_sale" : "7900",
                "price_ship" : "2500",
                "ship_price_type" : "1"                     
            }
        },

        {
            "deal" : {
                "deal_id" : "2",                        
                "qty" : "1",
                "option_price_sale" : "3880",
                "price_ship" : "2500",
                "ship_price_type" : "1"                     
            }
        }
    ];

Furthermore, it is possible to add new objects using the push method:

deal_info.push({"3" : {
            "deal" : {
                "deal_id" : "3",                        
                "qty" : "1",
                "option_price_sale" : "3880",
                "price_ship" : "2500",
                "ship_price_type" : "1"                     
            }
        }});

Answer №2

let discount_info = {};

discount_info["42"] = {};
discount_info["42"]["offer"] = {};

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

Jquery events continue to accumulate without initiating until the preceding event has completed

Looking at the code below, it essentially fades all the images to 70% within the contact class. When an image is hovered over, its opacity changes to 100%. If multiple images are hovered over or multiple hover events occur in succession, the events will st ...

Column alignment issue detected

Can you help me with aligning the data in my column status properly? Whenever I update the data, it doesn't align correctly as shown in the first image. https://i.stack.imgur.com/300Qt.png https://i.stack.imgur.com/4Dcyw.png $('#btn_edit' ...

I keep encountering an error that says "ReferenceError: localStorage is not defined" even though I have already included the "use

I have a unique app/components/organisms/Cookies.tsx modal window component that I integrate into my app/page.tsx. Despite including the 'use client' directive at the beginning of the component, I consistently encounter this error: ReferenceErr ...

Transform a jQuery element into an HTML element

I'm facing a dilemma where I have a jQuery element that needs to be passed into a function that only works with HTML elements. How can I efficiently convert the jQuery element into an HTML element? ...

Differences between throwing errors, returning error objects, and using callbacks in Javascript

Currently, I am developing an assembler and simulator for a simplistic assembly language that my computer science students use during their classes. The project is being written in JavaScript with the intention of creating a user-friendly interface in the ...

Is it possible to organize multiple tables within a single JSON structure without relying on JArray?

I am currently developing a program similar to IMDB, where I am showcasing individuals with a filmography, discography, and more. After setting up my view in MVVM, I am now working on serializing my JSON data to align with it. At the moment, I am consolida ...

JavaScript - returning a false error

I'm experiencing an issue with my form validation function that is supposed to check for empty fields by looping through the form elements. Here's the code snippet: function validateForm(ourform){ var formElements = document.getElementById(our ...

Creating a Website for Compatibility with NoScript

During my journey of building a nameplate site from the ground up for myself, I have delved into the realms of learning and establishing my online presence. The highlight of my project is a sleek tabbed site that employs AJAX and anchor navigation to seaml ...

Managing loading and changing events using Angular, jQuery, and Web API

I am populating a dropdown select using ng-repeat. <select onchange="ChangeMonth()" id="month"> <option ng-repeat="(key,val) in Months" ng-selected="val==ActiveMonth" value="{{key}}"> {{val}} ...

What is the best way to have a variable adjust each time a coin is inserted until it reaches a specific value?

I have developed a unique coin box that recognizes the value of each coin inserted. Users can now pay for a service that costs 2.0 € by inserting coins of various denominations such as 2.0 €, 1.0 €, 0.50 €, 0.20 €, and 0.10 €. In my react-nati ...

Trying to filter out repetitive options in an autocomplete box

Using the jQuery autocomplete plugin, I am trying to display 5 unique search results. Initially, I achieved this by limiting the stored procedure to return only 5 results, but now I want to remove duplicates while still showing 5 distinct results. I'm ...

How to Extract a URL from an Anchor Tag without an HREF Attribute Using Selenium

How can I make a link, which normally opens in a new window when clicked, open in the current window instead? The link does not have an href attribute, only an id and a class. For example: <a id="thisLink" class="linkOut">someLinkText</a> I r ...

Having trouble with the filtering feature in Material UI Datagrid

I'm currently using Material UI Data Grid to display a table on my website. The grid has an additional filter for each column, but when I click on the filter, it hides behind my Bootstrap Modal. Is there a way to bring it to the front? https://i.stac ...

Enabling the execution of returned scripts using JQuery AJAX

I have a hyperlink that I need to send a DELETE request using JQuery through AJAX. if(confirm("Do you wish to proceed?")) { $.ajax({ url: $(this).attr("href"), type: 'DELETE', success: function(result) { // Perform act ...

Upgrade to the latest version of MaterialUI - V4

After attempting to upgrade materialUI/core from version 3.9.3 to 4.4.1 and materialUI/icons from version 3.0.2 to 4.4.1, I encountered the following error: Error: TypeError: styles_1.createGenerateClassName is not a function I am currently importing crea ...

An easy way to enable mobility for BootstrapDialog on mobile devices

Currently, I am utilizing the library available at https://github.com/nakupanda/bootstrap3-dialog in order to create a dialog box. However, my issue arises when viewing the dialog on mobile devices where it exceeds the screen size. On desktops, adjusting t ...

Exploring the possibilities of combining Selenium Code and F# with Canopy

Currently, I am facing the challenge of incorporating Selenium code into my F# project while utilizing the canopy wrapper. Canopy relies on Selenium for certain functions. My main struggle lies in converting Selenium code from Java or C# to fit within an ...

Extracting information from Semi-Structured Data using Snowflake

I'm attempting to extract the health information from Snowflake's semi-structured data stored in a variant column named extra within table X. An illustration of the code is displayed below: [ { "party": "[{\"class ...

Sending data to a MySQL database using AJAX with PHP

While attempting to use ajax to insert a value in PHP, I am encountering an issue where the data is not getting inserted into the database. The code snippet I am using was sourced from answers provided on this site. Can someone please point out where I m ...

The proper way to retrieve data using getServerSideProps

Encountering an issue with Next.js: Upon reaching pages/users, the following error is displayed: ./node_modules/mongodb/lib/cmap/auth/gssapi.js:4:0 Module not found: Can't resolve 'dns' Import trace for requested module: ./node_modules/mon ...