Using a string as a key for an object's property

function createObject(argName, argProperties){ 
       var object = {};
           object[argName] = argProperties;
}

I am calling my function in the following manner.

   createObject('name', objectProperties);

The issue I am encountering is with the structure of the resulting object. It currently looks like this:

{"argName":{"Properties":"Value"}}

My desired output would be as follows. This is a general class that I am using to create multiple request objects.

{"name":{"Properties":"Value"}}

Answer №1

To assign a value to an object using square brackets:

object[arg1] = arg2;

For more information, check out:

Answer №2

item[property] = value;

This method should be effective.

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

What is the best location to store the JWT bearer token?

Exploring options for storing JWT tokens in a Bearer header token for my application. Looking for the most efficient storage mechanism. Would love some insight on how to accomplish this using jQuery. Any recommendations for a secure method? ...

Utilizing jQuery to display an element once JSON data is returned to the page

When a user clicks on an image on page 1, they trigger a pop-up window that allows them to upload an image file. The PHP script responsible for handling this action will return a JSON-encoded string containing a variable named 'id'. Here is an e ...

Using React to Render a Component with Local Storage Information

I'm in the process of developing a history list component for a form within a react application and encountering difficulties with local storage. The goal is to display a list of previous user inputs from the local storage data. My current approach i ...

struggling to develop a sophisticated 'shopping cart organization' program

I am in the process of creating a database for video spots, where users can view and modify a list of spots. I am currently working on implementing a cart system that automatically stores checked spot IDs as cookies, allowing users to browse multiple pages ...

Code written in JQuery functions correctly when executed within the console, yet encounters issues when located within the script tag or

I'm attempting to build an online drawing tool reminiscent of an Etch-A-Sketch by using a grid of div elements. However, I'm facing an issue where the code responsible for highlighting the boxes when the mouse hovers over them (using the mouseent ...

How can I efficiently add multiple items to an array and store them in async storage using React Native?

I am trying to store multiple elements in local storage using React Native. I found some helpful documentation on how to do this here. Could someone guide me on the correct way to achieve this? Here's a snippet of my code: My current approach const ...

Troublesome button appearance

I set up two sections to gather user information using buttons. My goal is for the button styles to change when clicked, making it clear to users which option they have selected, even if they switch between sections. However, I encountered an issue where ...

Are you feeling lost when it comes to Javascript? How is it possible for a boolean function to function as a

Preparing for an upcoming interview, I'm diving back into the world of JavaScript. Recently, I came across an interesting blog post that mentioned: "The delete operator returns true if the delete was successful." The blog then provided an example ...

The 'length' property is not found within the 'HTMLElement' type

Can someone assist me with looping over the number of nav-items I have? I am encountering an error that says: Property 'length' does not exist on type 'HTMLElement'. I understand that changing document.getElementById('nav-item) to ...

"Encountered issues during compiling: X ERROR found at line 9 in the Header.js file, spanning from characters

An error occurred while compiling the module. The following message was displayed: Module not found: Error: Can't resolve '@mui/icons-material/Search' in 'C:\Users\pande\Documents\slack-clone\src\component ...

Updating Rails record using Ajax with select_tag and onchange functionality

I'm working on an index view where I want to update a table data dynamically using select_tag onchange feature without the need to refresh the page. Do I need to use Coffeescript to detect the onchange event, capture the new value, and then send it to ...

Getting access to props within a child component's mounting phase

It is commonly understood that when the child component is mounted before the parent component, trying to access props in the child component's mounted period will result in null. However, I recently came across a scenario where props were successful ...

"Experience the magic of Datepicker jQuery with just a click

Does anyone know how to capture the click event when a day is selected using jQuery Datepicker? Is it possible, and if so, how can I achieve this? https://i.sstatic.net/ZG4r8.png Here is the HTML code snippet: Date From : <input type="text" ...

Using Highcharts within a Vue.js component

I'm having trouble creating graphical components with Highcharts and Vue.js because I can't figure out how to dynamically set the id attribute that Highcharts needs to render properly. Does anyone know how to set the id dynamically? This is the ...

Initiating PHP outcomes with the integration of JQUERY and Bootstrap

Currently, I am working on a Twitter search functionality that allows me to search for any content on Twitter. While the feature is functional, I am facing some challenges with displaying the results in the desired format. Ideally, I would like the results ...

React - output from forEach() function is not defined

I am facing a challenge in rendering prop values from a nested object. There are two issues that I'm encountering: 1. The JSX code line (with variables) is not showing up in the browser 2. When I console log the variables with .forEach() methods, th ...

Identifying Flash content in a unique way

In my dynamic page (let's call it myFlashContainer.jsp), the Flash content changes based on the link that is clicked. The code responsible for rendering the Flash looks like this: <object height="100%" align="l" width="100%" id="player" codebase= ...

No response text returned from the local Ajax request

Currently, I am facing a challenge while attempting to send an ajax call from the client to my server containing data related to an input parameter. The issue is that although I can view the data in my server's console, it does not display in the brow ...

Node/Express API returning empty body when being accessed through fetch or axios requests

Currently working on integrating an API call in a React app using Node/Express. No matter where I place the fetch/axios call, the parsed body always shows up as undefined in my controller. Yesterday, I was experimenting with fetch but decided to switch to ...

Retrieving JSON Array Information Using PHP Objects

stdClass Object ( [id] => 8586332 [email] => <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f373a3333301f32263a323e3633713c3032">[email protected]</a> [optInType] => Unknown [emailType] => Html [ ...