Using JavaScript, is there a way to modify a JSON parameter within an API?

My API provides JSON data structured like this:

[{"NAME":"john","SURNAME":"johny","ADULT":"3","CHILD":"3","BABY":"0",}]

In my JavaScript function, I need to send a request to a web service that will update the value of "BABY" from "0" to "1". Can this be achieved without relying on jQuery?

Answer №1

Here we have a typical JavaScript array with a common JavaScript object nested within. If I understand correctly, to modify the "BABY" property in the provided code snippet, you simply need to access it and update its value:

// responseData is equal to [{"NAME":"john", ... ,"BABY":"0"}]
responseData[0].BABY = '1';

Answer №2

To retrieve the object in the array, make sure to use arr[0]. Since the data type of BABY is a string value of 1, you should set it using arr[0].BABY = "1" rather than arr[0].BABY = 1.

var arr = [{"NAME":"john","SURNAME":"johny","ADULT":"3","CHILD":"3","BABY":"0",}];

arr[0].BABY = "1";
console.log(arr);

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

Adding flair to a fresh element and then removing it upon its inception

I'm working with a JavaScript code that creates a new element when a button is clicked. I have a question about it. Here's the JavaScript code snippet: var comment = document.querySelector("#AddComment"); var req = new XMLHttpRequest(); if(comm ...

Learn how to effectively deserialize a JSON string with an array into a specific class using GSON

I need help on how to serialize and deserialize this specific JSON format into a Java class. Anyone have any code samples or suggestions? [ { "topic": "this is my topic" }, [ { "name": "John" }, { ...

What impact does nesting components have on performance and rendering capabilities?

Although this question may appear simple on the surface, it delves into a deeper understanding of the fundamentals of react. This scenario arose during a project discussion with some coworkers: Let's consider a straightforward situation (as illustrat ...

Obtain JSON string from a Play Framework controller

I'm currently working on an ajax POST request from the client. The Play Framework controller I have is making a request to a cross domain server that in return provides JSON data. My intention is to send this JSON data back to the client. However, whe ...

Shopping cart feature in PHP - Ajax response only functioning once

I'm in the process of developing a shopping cart. I start by adding products to the cart, then use Ajax to dynamically update the quantity when it changes. Everything works smoothly after the first change, but subsequent quantity updates do not refres ...

Is there a way to customize the outlined color of an input adornment in MUI?

Looking to customize the default blue color in a Form Control outlined variant, but can't figure out how. I was able to do it with a regular TextField, but this one is a bit trickier. <FormControl variant="outlined"> < ...

React Navigation Item Toolbar Misplacement

I've been trying to align the navigation item with the logo on the same line within the toolbar, but I'm facing an issue where they keep appearing in different rows. To see the error for yourself, check out the code sandbox here. This is how I s ...

having trouble loading marker in react leaflet within next.js

I am facing difficulty in implementing react leaflet Marker in my next.js project. Below is the code snippet where I have included my map component: const MapSearch = dynamic(import('../../components/Map'), { ssr: false, loading: () => ( ...

The method of AJAX Pattern for users to make interactive decisions

Currently, I am exploring different strategies to create interactive user decisions. My project involves a rather extensive form that users need to fill out. Upon submission, an AJAX-Post-Request is transmitted to the server. While some fault checks can be ...

Is the API providing a response in the form of an HTML document

I just created a cutting-edge React application that leverages the power of "fetch" to fetch data from an API. Within my App component in the "App.js" file, you can find the following code snippet: function fetchData() { fetch(`WORKINGURLWITHAPIKEY`) ...

Utilizing the Express-busboy npm package to generate a new directory within the public folder of

While working on my controller, I encountered an issue when trying to readFile sent from the browser via AJAX. Unexpectedly, a directory was created in my public folder with a name like '3d6c3049-839b-40ce-9aa3-b76f08bf140b' -> file -> myfile ...

Ways to insert text at the start and end of JSON data in order to convert it into JSONP format

Currently, I am working on a project where I need to add a prefix "bio(" and a suffix ")" to my JSON data in order to make it callable as JSONP manually. I have around 200 files that require this modification, which is why I am looking for a programmatic ...

Leveraging the source of an image from asset variables

Lately, I've been experiencing issues with displaying images on my page, specifically when trying to show a list of images. The problem arises when attempting to store the image URL in a variable or object instead of hardcoding it directly into the s ...

What is the best way to combine a string that is constructed across two separate callback functions using Mongoose in Node.js?

I am facing a situation where I have two interconnected models. When deleting a mongo document from the first model, I also need to delete its parent document. There is a possibility of an exception being thrown during the second deletion process. Regardl ...

What is the most effective way to reduce the number of http requests caused by onChange events in Material UI Slider?

Is there a way to optimize my request handling? In my React project, I am currently utilizing Material UI's slider. With the onChange property, I am making HTTP POST requests whenever the slider is moved. Here is a snippet of the code: <Slider ...

Javascript/jquery functions perfectly in all browsers except Firefox

This particular piece of code seems to be functioning properly in Internet Explorer 8, Chrome, and Safari, however, it is not working as expected in Firefox: <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></scr ...

Custom input field in Material UI not communicating properly with Redux form

I am currently utilizing Material UI and React to implement a custom input field. While using redux form for form validation, I have encountered an issue where the onBlur and onFocus events are not being dispatched successfully. Interestingly, if I switch ...

Is there a method to automatically execute an 'onchange' function whenever a value is updated due to an AJAX response?

Many drop down menus are present on my page; <... onchange="callthisfunction(...)"...> While a manual change easily triggers the function, using ajax to load saved data does not register the value as changed and the function is not triggered. Is th ...

How do I submit my form data as a JSON object using AngularJS?

I'm currently in the process of developing my first Angular app and trying to incorporate form submission into JSON data. Below is the form I have created: <span ng-show="show"> <form name="inputForm" class="form-group" ng-controller="For ...

Making React function properly on GitHub Pages

I have followed all the steps and even consulted multiple tutorials that all say the same thing, but when I try to run "npm run deploy" I encounter an error and nothing happens. This is the error message: [email protected] deploy A:\Documents&bs ...