Manipulate a JSON object with JavaScript

Struggling to find a solution on my own.

In my hidden field, I have some JSON data stored.

To retrieve this data, I'm using the following syntax:

 $(document).ready(function() {
var data = $("#result").text();
var j = JSON.parse(data);
j.my_item.total_price==="2222";
console.log(j.my_item.total_price);

});

Although the variable 'j' displays the correct information, I'm unsure how to update the total_price value.

Any suggestions on how to go about updating the total_price?

Answer №1

To set a value to an object property, you can use the following methods:

obj.my_property = "value";

Alternatively, you can also use array-like syntax:

obj['my_property'] = "value";

You can also mix and match these methods:

obj.my_property['sub_property'] = "value";
obj['my_property'].sub_property = "value";

Answer №2

$(document).ready(function() {
var info = $("#result").text();
var k = JSON.parse(info);
k.my_item.price="2222";
console.log(k.my_item.total_price);

});

=== isn't just an assignment operator, it's a type-strict comparison operator.

Learn more about it here:

Difference between == and === in JavaScript

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

The "keydown" event in React will not alter the state

I am currently developing an application that requires me to track the keys pressed by the user. I am utilizing keydown and keyup events for this purpose. However, I am facing a challenge where I do not want the same key to be registered multiple times whe ...

When a radiobutton is clicked, a jQuery call to a PHP function triggers an AJAX request which results in a JavaScript function becoming unrefer

Currently, I have a situation where two radio buttons are representing different products. When one of them is clicked, the goal is to update the price displayed on the website based on the selected product. Everything seems to be working fine when using t ...

Apple Automation: Extract a targeted string from text and transfer it to a different spot within the page

Apologies for my lack of expertise in this area, but I hope to convey my question clearly. I am working on a form where I need to input text in order to copy specific items and paste them elsewhere on the same webpage. For example, if the input text is " ...

Using React refs to target multiple elements dynamically with the help of the map

My code effectively catches when the dropdown is clicked outside, and it's working well: displayOptions() { return _.map(this.props.selections, (option, index) => { return ( <div className="ui icon top dropdown" ...

How can Vue detect modifications made in the edited field?

I am facing an issue with tracking changes in a specific object. Here is the structure of the object: users: { email: '', password: '' } My goal is to detect any edits made to the keys within the users object and store the key ...

Tips on exposing promise data object in the controller and transferring data from the promise to a new function

After creating an angular controller and service to extract data from a JSON file, everything seemed to be working smoothly. However, I encountered an issue when attempting to assign this data to a new object. My goal is to implement a new function within ...

The Link Breaks the Overlay Hover Effect

Currently, the code functions as intended when you hover over or touch the thumbnail, an overlay will appear. The issue lies in the fact that to navigate to a specific URL, you have to click directly on the text. The overlay itself is not clickable and ca ...

Should property paths be shortened by utilizing new variables for better organization?

Yesterday, someone asked me why I would introduce a variable to shorten the property path. My answer was simply that it feels easier to read. Now I am curious if there are any objective reasons to choose between the two options listed below (such as memory ...

The initialization of the R Shiny HTML canvas does not occur until the page is resized

I am currently facing an issue while integrating an HTML page with a canvas into my shiny R application using includeHTML(). The packages I am using are shiny, shinydashboard, shinycssloaders, dplyr, and DT. Everything is working perfectly fine except for ...

Definitions that are displayed dynamically when hovering over a particular element

I am seeking a way to implement popup definitions that appear when a div is hovered over. My website showcases detailed camera specifications, and I want users to see a brief definition when they hover over the megapixel class called '.mp'. One o ...

The contact form displays a confirmation message indicating successful submission, however, it fails to actually send the email

Having issues with a PHP script I created for the contact page on my website. After a user fills out and submits the form, they see a success message but the email is not sent. Can someone review this script and point out where I went wrong? <?php ...

Submit the image to the server independently from the form

Currently, I am faced with an issue while working on image upload functionality in my React app using Node and Express for the backend. Within my React component, I have a form containing an input type file and a submit button. My goal is to first upload t ...

Step-by-step guide on how to include the "content-based-recommender" package in a Reactjs project

I recently started learning React.js and I'm interested in using the "Content-Based Recommender" package from npm. However, after downloading it, I encountered an issue with importing it. The documentation suggests the following: const ContentBasedRec ...

Compare the selected values of radio buttons with an array and find matches

I have multiple radio button groups, where each selection in a group corresponds to predefined values in an array. For example, selecting option 1 in Group 1 will increment values A and B by 1, while selecting option 2 will increment B, C, and D. The goal ...

SyntaxError: Unexpected symbol

I have an issue with the following code: let op = data.map(({usp-custom-90})=> usp-custom-90 ) When I run it, I encounter the following error: Uncaught SyntaxError: Unexpected token - I attempted to fix it by replacing the dash with –, but t ...

Is it possible to optimize the performance of my React and TypeScript project with the help of webpack?

I am working on a massive project that takes 6 to 8 minutes to load when I run npm start. Is there a way to speed up the loading process by first displaying the sign-in page and then loading everything else? ...

Click anywhere outside the sidemenu to close it

I want the menu to behave like the side menu on Medium. When the side menu is open and the user clicks outside of #sidebar-wrapper, the side menu should close. Currently, I have to click the toggle X to close the menu. html <a id="menu-toggle" href="# ...

Is there a way to link the req.session.user from express-session to a chai-http request?

When it comes to my API, it relies on the express-session module for authentication. Each request is verified based on whether the req.session.user object exists within the request. The implementation can be seen in the snippet below: app.use(function(req ...

Tips for transferring a JavaScript variable to PHP with AJAX

I am encountering an issue while trying to transfer a JavaScript variable, obtained from clicking a button, to PHP in order to execute a MySQL query. Here is my code: function ajaxCall(nodeID) { $.ajax({ type: "POST", url: "tree.php", data: {activeNode ...

Is it possible to make a call to an endpoint from within the node project?

I'm currently working on a MERN app that dynamically adds meta tags to React pages without using server-side rendering. In order to achieve this, I need to extract the query parameters from the main server file and assign the relevant metadata content ...