Is it possible to modify JSON using JSON in JavaScript?

Let's consider a JSON data scenario:

olddata = {
    userNname : "joeydash",
    sex : "female",
    email : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccada9fdfaaefcfcfd8cbfa1ada5a0e2a5a5b8a1e2adafe2a5a2">[email protected]</a>"
};
dataToEdit = {
    name = "Mritunjoy Das",
    sex = "male"
};

Are you aware of a JavaScript function that can achieve the following?

newData = doSomething(oldData,dataToEdit);
console.log(newData);

The expected output should be as follows:

{
   userNname : "joeydash",
   name : "Mritunjoy Das",
   sex : "male",
   email : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="254440141347151514655648444c490b4c4c51480b44460b4c4b">[email protected]</a>"
}

Answer №1

olddata = {
    userNname : "joeydash",
    sex : "female",
    email : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a7a3f7f0a4f6f6f786b5aba7afaae8afafb2abe8a7a5e8afa8">[email protected]</a>"
};
dataToEdit = {
    name : "Mritunjoy Das",
    gender : "male"
};
var newData = Object.assign(olddata, dataToEdit);
console.log(newData);

Answer №2

Give this code a try:

function updateData(existingData,newData){
     for (var property in newData) {
         if (newData.hasOwnProperty(property)) {
              existingData[property] = newData[property];       
         }
     }
     return existingData;
}

Remember to replace = with : like so:

newData = {
    name : "Luna Smith",
    gender : "female"
};

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

Tips for effectively managing dynamic xpaths

When conducting a search operation, I am required to select the text that is returned as a result. Each search will produce different xpaths. Below are examples of various xpaths returned during a search: .//*[@id='messageBoxForm']/div/div[1]/di ...

Angular Object Error: Unhandled Exception

As someone who is new to Angular, I am still trying to grasp how it functions. However, I have encountered an issue early on that is causing the below code to produce an "Uncaught Object" error in the console and disrupt Angular functionality. It seems t ...

Retrieve data from a JSON object and separate them into columns based on a specific identifier

I am working with a database that stores JSON arrays of objects in a column. My goal is to create columns based on the keys of these objects when the object's id meets certain criteria. The structure of my documents table is as follows: id | name ...

The rendering of the input dropdown control in Angular JS is experiencing difficulties

I am currently using your Angular JS Input Dropdown control, and I have followed the code example provided on your demo page in order to implement the control on a specific page of my website built with PHP Laravel. However, I have encountered an issue dur ...

What is the best way to retrieve an object from a POST request using Angular AJAX calls in a NODEJS environment?

When the button is clicked, a method will be called. The code for this is as follows: .controller('templeDetailsList', function ($scope, $http, $ionicModal) { $scope.starclick = function(){ var newFav = [{ ...

Tips for displaying a setCustomValidity message or tooltip without needing to submit the event

Currently, I am utilizing basic form validation to ensure that the email entered is in the correct format. Once validated, the data is sent via Ajax for further processing. During this process, I also check if the email address is already in use and whethe ...

Is there a way to dynamically load a file on scroll using JavaScript specifically on the element <ngx-monaco-diff-editor>?

I've been attempting this task for over a week now in Angular without success. Would someone be able to provide guidance? The onContainerScroll() function isn't being triggered, and I'm considering using JavaScript instead. How can I achiev ...

Verify the presence of blank space with javaScript

I currently have a text box on my website. <input type="text" name="FirstName" value="Mickey"> My goal is to prevent the user from entering empty spaces, tabs, or new lines in the text box. However, I want to allow spaces between characters. Does a ...

The console displays "undefined" when formatting API data

I am attempting to format the data retrieved from an API since there is a lot of unnecessary information. However, when I try to display the formatted data in the console, it always shows as "undefined" or "null." I am importing and calling the fetch API ...

Use JavaScript to input array elements into a selection of cells in Excel

At this moment, the loop I am executing successfully retrieves the necessary values. However, I am facing challenges when it comes to inserting these array values into a range of cells. I keep encountering the following error message: Error message: "The ...

Develop a C# program that transforms a single list into a nested JSON API structure

I have a basic list retrieved from my database to my MVC API, here is an example query select school, class, pupil from area I am populating a list with this data foreach (DataRow row in table.Rows) { var area = new Area(); area.school = row.ItemA ...

When modifying a string in React, the edit always somehow ends up at the very tail end

In my ReactJS app, I have a Material-UI input element. Everything is working well except for one issue - when I try to edit the text in the middle of the string, the cursor always jumps to the end of the string after every letter I input. I suspect this m ...

Tips for adjusting the current window location in Selenium without having to close the window

I am currently working with seleniumIDE My goal is to have a Test Case navigate to a different location depending on a condition (please note that I am using JavaScript for this purpose, and cannot use the if-then plugin at the moment). I have tried using ...

How much time can the browser dedicate to running JavaScript before moving on to loading a new page?

The webpage contains multiple hyperlinks. I want to monitor user clicks on these links. Whenever a user clicks on a link, an Ajax request is sent to my server for processing. The server then returns relevant data, which is further processed on the client ...

Having trouble accessing a CSV file with JQuery and FileContentResult?

How can I make an ajax call without using ActionLink in my controller? Here is a snippet of my controller code: public IActionResult ExportUsers(List<string> listOfEmails) { /*some data processing*/ return File(result, "text/csv", ...

Fetching information from a data object using asyncData in Nuxt

Is there a method to retrieve object properties as keys from the asyncData() function? data() { return { bookmark_btn: { status: null, loading: false } } } I attempted to access data object properties in the following ...

IE8 encountering issues with Jquery ajax() request

My current issue involves submitting data using ajax from forms with a class of ajax. This functionality works flawlessly in Firefox, Safari, and Chrome, but is unsuccessful in Internet Explorer. ajax: function() { $('form.ajax').live(&apo ...

Sharing data in JavaScript functions

In order to use certain variables in two separate functions, there are some considerations to keep in mind. The first function is responsible for calculating and displaying a conjugated verb using these variables. The second function checks the user's ...

The comparison between "rxjs-tslint" and "rxjs-tslint-rules" npm packages

Previously, I utilized the rxjs-tslint-rules package to identify RxJS-related issues in my projects. This package was included in the devDependencies section of my projects' package.json files. Now, there is a new rxjs-tslint package that introduces ...

JavaScript loop through an array of objects

Is there a way to loop through and retrieve all of the addresses? const name = { john: [ { age: 21, address: 'LA', } ], sam: [ { age: 26, address: 'California' } ] } I have tried using th ...