Challenges with Javascript arrays

let dataArray = [];
let id = 23;
let num = 12;
let dogTrialSet = 15;
let catTrialSet = 23;

encountering an issue here

dataArray.push({id:{num:num, dogTrialSet:dogTrialSet, catTrialSet:catTrialSet}});

sending AJAX request to PHP

[dataArray] => Array
    (
        [0] => Array
            (
                [id] => Array
                    (
                        [num] => 12
                        [dogTrialSet] => 15
                        [catTrialSet] => 23
                    )

            )

    )

I'd like [id] => Array(...) to be changed to [23] => Array(...);

Thank you

Answer №1

If you want to assign a key dynamically, consider using a variable and the subscript notation:

let newRow = {};
newRow[customId] = {number:number, dogTrials:dogTrials, catTrials:catTrials};
dataset.push(newRow);

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

Combining Arrays with Matching Index in Objective-C

Can anyone help me with merging two arrays with the same index in JSON format? Here is an example of my JSON data: "tier_info": [ { "tier_id": "1", "tier_name": "tier-1", "price": "3.9", "ios_id": "tier-1", "and ...

What sets the statements of ( for... in ) and ( for... of ) apart from each other?

Although I am familiar with the for... in loop (which iterates over keys), I recently came across the for... of loop for the first time, which apparently iterates over values. I find myself confused by this for... of loop. var arr = [3, 5, 7]; arr.foo = & ...

Ways to assign sx property to Icons using menu.map method in Material-UI?

Currently, I am working on designing a menu with icons in Material UI. Everything is functioning properly, but I am facing a challenge in understanding how to pass the sx prop into the Icon when using the map function. Here is an example: export const Me ...

Transforming an ArrayList into an Array within a JSP file

In my quest to transfer values from a Resultset in Java to a JSP using an ArrayList, I have encountered some challenges. After converting the ArrayList to an Array, I can extract the values successfully, but the format is not ideal. Instead of the curren ...

The server is not being reached by the Ajax PUT method

Having some issues with my login form when attempting to use the PUT method for logging in and storing the username. It seems like it's not communicating with the server properly and I'm unable to log in. Any help would be greatly appreciated. I ...

Is there a way to assign a key value to this code without resorting to a multidimensional array?

As I dive into OOP with PHP, I'm starting to grasp the concepts, but there's a particular piece of code that's not sitting right with me. It stems from a tutorial on constructing a login system, where the errors are displayed at the top of a ...

Is there a way to customize the progress bar percentage in Ant design?

I am currently utilizing React JS and importing the Ant Design progress component (refer to https://ant.design/components/progress/). However, I am facing difficulties in dynamically editing the percentage of the progress bar using JavaScript after it has ...

Utilizing the angularJS API to retrieve data

I am completely new to AngularJS and feeling overwhelmed. So I created this file to fetch data from an API with a time filter. forecast.js (function() { angular.module('application').factory('Forecast', ['$http', '$q& ...

ReactJS Material-UI Tooltip and Popper overlapping issue

How can I make the MUI tooltip appear beneath the MUI Popper, with the popper overlapping the tooltip? Is there a way to modify the z-index for only a specific portion of the elements without affecting the global styles when using external CSS? Here is a ...

Loading remote content on a server for my Firefox OS application - On the Web and FxOS device

I haven't come across this issue in any forum, so I decided to reach out here. I'm encountering a problem with my FirefoxOS app on my FirefoxOS device (Geeksphone Developer Preview) when trying to retrieve remote content from the server. I am m ...

Are there any ways to bring visual attention to a GoDaddy template's SEND button using HTML or JS when the original code is unavailable?

I'm currently working on a GoDaddy website using a template that doesn't clearly highlight the SEND button in a Contact Us form. In order to comply with WCAG accessibility standards, active elements must have visible focus indicators. However, si ...

What may be causing the lag in the translation within ThreeJS?

Just started diving into ThreeJS and discovering its capabilities. Playing around with a simple example of creating a white dot and applying a basic translation. let dotGeometry = new THREE.Geometry(); let dotMaterial = new THREE.PointsMaterial({ size: ...

Changing the color of tabs using inline styles in material ui does not seem to work

I am brand new to using material ui and have been attempting to alter the colors of the selected tab. Currently, the color is a dark blue shade and I am aiming to change it to red. I tried applying inline styles, but unfortunately, there was no change. C ...

Anticipating the outcome of a function in asynchronous JavaScript

After taking a break from web development for a couple of years, I recently dove back into it. While I dabbled in AngularJS before, I have now shifted my focus to Angular2. My current challenge revolves around handling asynchronous JavaScript. Despite enc ...

How can one utilize Codemirror code folding while avoiding the use of "[ ]"?

I am looking forward to implementing Codemirror codefolding for folding only braces { and }, as well as comments. However, I am facing an issue where it also folds square brackets [ and ]. Since square brackets are usually part of one-line statements, I do ...

Tips for converting an Elementor section into a clickable button

Link: I have a section on my website that I want to turn into clickable buttons. Using WordPress with the Elementor plugin, I have already added some custom CSS to make it appear clickable, but now I need to add the actual functionality. I know that JavaS ...

What causes all methods within a Vue template object to trigger automatically upon clicking a button?

I added a click event to the button element which calls the increase method to update the counter. The result() and result2() methods are called when the page loads, but they also get called automatically when the button is clicked. I'm wondering why ...

Utilizing Jquery to extract a specific string from a URL and fetch a remote element

Recently delving into Jquery, I'm in search of a code snippet that can capture the current page URL and load a remote element if it contains a specific string. For instance: Consider these sample page URLs: "http://......./Country/AU/result-search- ...

Dealing with non-existent property in TypeScript while working with JavaScript libraries

Currently, I'm utilizing TypeScript in a Vue.js application and aiming to implement the drop event to initiate image uploading as shown below: handleDOMEvents: { drop(view, event) { const hasFiles = event.dataTransfer && ...

Error encountered in processing array string

I have created an array as shown below, but I am facing issues with parsing it using json_encode. Is there something that I am overlooking? Any help would be appreciated. Here is the PHP code snippet: <?php $i=0; $result = shell_exec('scripts/fe ...