JavaScript: Identify the variable that has been updated

When checking for changes in the values of a couple of variables, I use the following condition:

<%
if (ctx.recipient.@firstName != ctx.recipient.@firstName_init || ctx.recipient.@lastName != ctx.recipient.@lastName_init || ctx.recipient.@emailPreferredName != ctx.recipient.emailPreferredName_init) 
    {       
        document.controller.setValue('/ctx/vars/piiChanges',1);     
    }
%>

I am looking to identify which specific condition has had its value changed out of the 3 conditions. How can this be achieved?

Below is the XML payload being used:

<recipient JOB_TITLE="" 
       blackListEmail="0" 
       cmOneID="0" 
       company="" 
       email="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8acbda5a5b188acbda5a5b1e6aba7a5f9">[email protected]</a>" 
       emailPreferredName="Dummy1" 
       firstName="Dummy1" 
       id="13246096" 
       initiative="" 
       investorType="" 
       jurisdiction="" 
       lastName="Dummy1" 
       lawfulBasisLabel="None" 
       lawfulBasisName="none" 
       leadScore="0" 
       origin="" 
       status="0" 
       statusLabel="Other" 
       statusName="other" 
       _operation="update" 
       _key="@id" 
       firstName_init="Dummy" 
       lastName_init="Dummy" 
       email_init="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="482c3d252531082c3d252531662b2725">[email protected]</a>" 
       emailPreferredName_init="Dummy" 
       blackListEmail_init="0">
<postalAddress addrDefined="0"/>

Answer №1

To retrieve a list of keys that have changed in the data object, you can iterate through the properties of the object. Here is an example snippet to achieve this:

function findChangedKeys(target) {
  const changedKeys = [];

  // Iterate over each property key of the target object
  Object.keys(target).forEach(key => {
    // Compare non-init values with their init counterparts to identify changes
    if (!key.endsWith("_init") && target[`${key}_init`] !== target[key]) {
      changedKeys.push(key);
    }
  });

  return changedKeys;
}

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 significance of declaring a constant array in JavaScript?

Does declaring an array as a constant in JavaScript prevent it from changing size, or does it mean that the values inside the array cannot be modified? handleClick(i) { const squares = this.state.squares.slice(); squares[i] = 'X'; this.setState( ...

Rendering an Image File from Database Using React JS and Node JS

I am encountering a problem with displaying an image file from my mongodb database on my Profile and EditProfile component. The issue arises when I upload the file using the Editprofile component - it saves to the database successfully. However, when I try ...

Encountering an error stating that 'coordinates should consist of an array with two or more positions'

Utilizing turf.js to generate a line depicting the path of an individual while their location is tracked. An array of coordinate arrays resembling Turf.js (lineString) is causing this error: Uncaught Error: coordinates must be an array of two or more posi ...

Inadequate data being sent to the server from Angular2 post request

Currently, I have a form field whose value I am passing to a service as this.form.value. However, when I log this.form.value on the console, I see Object { email: "zxzx", password: "zxzxx" }. Despite this, when I send the same data to the service and make ...

Reducing file size through compression (gzip) on express js version 4.4.1

My express js app is functioning as a web server, but I am having trouble with serving unzipped static content (js and css files). I have tried using compression from https://github.com/expressjs/compression, but it doesn't seem to be working for me. ...

The issue with using useState on arrays is that it is not functioning properly with React Hooks

const [LatestNews, setLatestNews] = useState([]); useEffect(() => { async function fetchLatestData() { const result = await database.collection('latestnews').get(); result.docs.forEach(doc => ...

Issues with the visibility of React Bootstrap components

I'm troubleshooting an issue with the carousel on my website. When I try to load the page, the carousel appears blank. To set up the carousel, I used npm to install react-bootstrap. The carousel code can be found at . Upon checking the web console, ...

What specific types of errors is this try-catch block designed to catch and manage?

// This function establishes a connection to MongoDB using Mongoose const connectToDatabase = (url) => { return mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => console.log('Conn ...

Confirming the information in two sections of a form

Could someone assist me with validating my form? Specifically, I need help ensuring that the house number field only accepts two numbers and the postcode field only allows 5 characters. I have implemented a pattern attribute for validation and I am curiou ...

Unable to assign headers once they have already been sent to the recipient - a Node.js error

Encountering an error message stating "Cannot set headers after they are sent to the client." I've researched and it seems like multiple callbacks may be causing this issue. However, I'm struggling to find a solution. Any assistance in resolving ...

Developing a universally accessible variable for utilization in various functions

I'm having trouble understanding why 'currentPos.LatLng' is undefined when trying to access it outside the function even though it's part of an object. I want to be able to retrieve the current position values to use them in another fun ...

The specified type '{ state: any; dispatch: React.Dispatch<{ type: string; value: any; }>; }' is not compatible with the expected type

I've been working on a UI layout that includes checkboxes on the left, a data table on the right, and a drop zone box. The aim is to keep the table data updated whenever a new file is dropped, and also filter the data based on checkbox selection. I ma ...

Display visual information without requiring the parameters to be filtered beforehand in vue.js

Upon opening my page, I encountered an issue where the graphics appear blank. This is because I set up the callback for generating graphic data through params request. I wish to first fetch the general data when the page opens, and only load with params w ...

Navigating to my own posts within Nuxt: A guide

Having trouble with internal links in my template, as they are not directing to the correct URLs. <nuxt-link :to="blog.url" >{{ blog.title }} by {{ blog.author }}</nuxt-link > In my data, I have the foll ...

Parameter for Ajax URL

As a beginner in the world of Ajax, I'm on a mission to grasp the inner workings of this technology. I came across a tutorial on w3schools that sparked my curiosity. In the code snippet below, the 'url' is defined as demo_ajax_load.txt. Wil ...

eliminating and adding a node

Is there a way to replace the existing span elements inside the div (<div id='foo'>) with newly created nodes? I have been looping through all the children of the div, using removeChild to remove each node, and then appending a new node in ...

Tips for disabling default browser input validation in React

https://i.stack.imgur.com/834LB.png Is there a way to remove the message "Please fill out this field" while still keeping the "required" attribute intact? I have my own validation system in place, so I need to use the "required" attribute to determine whe ...

Input form with multiple fields. Automatically display or hide labels based on whether each field is populated or empty

I am attempting to create a form where the placeholders move to the top of the input when it is focused or filled. However, I have encountered an issue with having multiple inputs on the same page. Currently, my JavaScript code affects all inputs on the pa ...

I am unable to store rich text fields using LocalStorage

My goal is to store form data in localStorage, but I'm facing an issue with rich text fields not saving their data. Regular HTML fields like textboxes work fine. In my Razor markup for the field (using MVC), here is an example: <div class="form-g ...

"Is there a way to ensure that jPlayer plays the same song even after a page refresh

I have been working on integrating jPlayer with Ajax to ensure that when the page of my website is refreshed, jPlayer will continue playing its current song seamlessly. I have successfully stored track information and the current track during refresh, ho ...