【问题标题】:javascript: detect which variable changed its valuejavascript:检测哪个变量改变了它的值
【发布时间】:2022-01-11 10:07:35
【问题描述】:

鉴于我有以下条件来检查几个变量的值是否发生了变化。

<%
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);     
    }
%>

如何检测所有 3 个条件值中的哪一个发生了变化?

这是我的 XML 负载

<recipient JOB_TITLE="" 
       blackListEmail="0" 
       cmOneID="0" 
       company="" 
       email="dummy@dummy.com1" 
       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="dummy@dummy.com" 
       emailPreferredName_init="Dummy" 
       blackListEmail_init="0">
<postalAddress addrDefined="0"/>

【问题讨论】:

    标签: javascript xml


    【解决方案1】:

    您可以遍历数据对象的属性并获取所有已更改键的列表。像这样的:

    function getChangedProperties(recipient) {
      const changedProps = [];
    
      // Loop over each of the property keys of the recipient object
      Object.keys(recipient).forEach(key => {
        // Check non-init values to see if their init counterparts are equal
        if (!key.endsWith("_init") && recipient[`${key}_init`] !== recipient[key]) {
          changedProps.push(key);
        }
      });
    
      return changedProps;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-24
      • 1970-01-01
      • 2014-09-25
      • 2011-11-24
      相关资源
      最近更新 更多