Modify the index in the creation of a json

I am trying to create a JSON object using the code below:

var myJSONObject = [];
var id = "1", value = "I'm a value !";
myJSONObject.push({id:value});

When I display this construction, it shows: [{"id":"I'm a value !"}]

However, I want it to display like this: [{"1":"I'm a value !"}] Is there a way to achieve this?

Answer №1

To properly access the data, you should use bracket notation:

var myJSONObject = [];
var id = "1", value = "I'm a value!";
var obj = {};
obj[id] = value;
// ^------- Make sure to use this notation!
myJSONObject.push(obj);

When retrieving a value with a numeric key, remember to use bracket notation as well:

console.log(myJSONObject[0][1]); // or "1"; both will output "I'm a value"

It's important to note that what you're referring to as a "JSON object" is actually a JavaScript array containing one JavaScript object. If you need JSON format (e.g., for server communication), you can do the following:

var jsonString = JSON.stringify(myJSONObject);

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

Enhancing Octahedron Geometry in Three.js: Incorporating Materials Array

Is it possible to apply different materials to each face of a THREE.OctahedronGeometry? I have successfully done this with box objects, but when I try with THREE.OctahedronGeometry, I only see the first material on all faces. How can I achieve this with ...

Is it feasible for a form button to perform multiple actions simultaneously?

I am interested in exploring the possibility of having a submit form button perform multiple actions. Currently, I have a custom form that is sent to a Google Spreadsheet using AJAX and I am also utilizing the Blueimp Jquery File Upload plugin. My goal is ...

Running of code in <script> tag

At what point does the code inside script tags start executing? Does it happen in order? <html> <head> <title>Canvas tutorial</title> <script type="text/javascript"> function draw(){ var canvas = document.getElementById ...

Manipulate the lines in an HTML map and showcase the distinctions between them

I've been searching through various inquiries on this particular subject, but none have provided me with a satisfactory response. I have created a map where I've set up 4 axes using the following code: function axis() { var bounds = ...

Data retrieval is currently not functioning, as React is not displaying any error messages

One of the components in my app is as follows: import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { compose } from 'redux'; import { translate ...

Addressing file routes within my personalized npm bundle

I am in the process of developing a custom npm package called packer and experimenting with a configuration similar to webpack, where users can install my CLI tool to build their packages. Here is an example of a package.json file for a test package using ...

What is the process for programmatically aligning two cards side by side using Material UI in React after retrieving data from an API?

I am currently working with data from an API that I need to display in the format of cards, arranged side by side. To achieve this, I have been using an array and passing the data to components programmatically. You can see the output image and my code bel ...

Can a link be generated that swaps out the original URL redirect for the following visitor upon clicking?

Can the program be fed with a list of links to automatically redirect to the next URL once clicked? In this setup, each visitor would only see one link and not have access to any other URLs in the chain. Is there a way to make this happen? Any suggestion ...

Displaying array elements on a webpage using JavaScript in HTML

Looking to display multiple blocks in HTML using a JavaScript array. The array contains names such as: var name=['amit','mayank','jatin'];. I need to repeat a certain portion of code in order to show the elements of the array, ...

Encountering problems when trying to open .dialog using JQuery

Hello everyone! I have an interface where, after a user logs in, their information is checked. If the user has not read the Terms of Service (TOS), then a dialog box should open. However, I am facing an issue as the dialog box never opens. Here is the cod ...

What is the best way to incorporate a particular locale from AngularJS I18n files with bower?

After successfully downloading the angular I18n repo by using bower install angular-i18n, it is now located in my bower_components directory and has updated the bower.json file with angular-i18n : 1.5.3 as expected. However, I am facing an issue where a s ...

What is the best way for me to retrieve JSON data from a secure https link using Python?

Currently, I am delving into the realm of Python and am interested in utilizing Twitter's streaming API. While I have successfully built a basic application that reads page content using urllib2, I have hit a roadblock with the secure connection requi ...

Is there a way for me to access a user control property directly from the client side?

I'm in the process of developing several user controls that will be derived from my base class, BaseControl, which is a subclass of UserControl. The BaseControl class contains important features that I will need to utilize, including a string property ...

How can I make the input box TextField in Material UI with React expand to full width when selected?

I am currently customizing a Material UI form where the input box (Text Field) starts off at a width of 200px. My goal is to have the input box expand to 100% width only when it is selected or clicked on. ... <FormGroup sx={{ maxWidth: "200px" ...

Converting Microsoft Word files into HTML format

Similar Question: Converting .doc to html using PHP I'm looking for a way to transform a word document into HTML format for embedding on a webpage. I'd like to accomplish this task using PHP. Any suggestions on how to achieve this? ...

Access and retrieve data from a string using XPath with JavaScript

My HTML page content is stored as a string shown below: <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <h1>This is a Heading</h1> <p>This is a paragraph.</p> </ ...

What is the best way to navigate from a button in NextJS to another page that I have built within the same application?

As I work on developing a website for a pet rescue charity using Next.js, I am facing an issue with getting my button or link to function correctly. Every time I try to navigate to another page within my Next.js app, I encounter a 404 error stating "This p ...

Is there a way to restrict the number of times I can utilize slideToggle function?

When I continuously click on a div element in an HTML website that triggers the .slideToggle() method, it will keep opening and closing for as many times as I click. The problem arises when I stop clicking, as the <div> continues to toggle open and c ...

Is there a way to make the hoverzoom effect only apply to a specific object instead of all objects?

I'm currently troubleshooting my code in order to implement a hover effect on Mui <cards> when a user hovers over the object. I have managed to make it work partially, but the issue is that the effect gets applied to all objects instead of just ...

The promise of a MongoDB connection with Node.js returns as 'awaiting fulfillment'

Greetings to all! A few weeks ago, I embarked on the journey of learning javascript, node.js and mongo. As a beginner, I have an interesting task at hand today. My goal is to add a simple document to the mongoDB and perform a conditional check. So here&apo ...