How to find different property of two objects.

I have two objects they all the same type. I want to compare the two objects and extract which properties are different. Besides using a loop to iterate, is there a better way?

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    If you want just to check whether the variables/datasets are different or not then convert the datasets into json using a!toJson() and then compare like

     

    a!tojson(a!map(key: 2, value: 2)) = a!tojson(a!map(key: 1, value: 2))

    If you want a list of attributes and values which vary between two datasets then a!foreach() is needed atleast once to parse and check each field in the datatype.

    Below is a solution with two expressions which can help with this part. In one expression we just have simple expression to compare 2 values which returns true if values dont match.

    In main expression we have code like below which filters and returns the field and values where match failed. 

    a!localVariables(
      local!map1: { name: "Alex", age: "22", dob: today() },
      local!map2: { name: "Alexa", age: "22", dob: today() - 4 },
      local!keys: a!keys(local!map1),
      a!foreach(
        local!keys,
        a!map(
          field: fv!item,
          value: filter(
            rule!HSS_CompareValues,
            merge(
              { index(local!map1, fv!item, {}) },
              { index(local!map2, fv!item, {}) },
              
            )
          )
        )
      )
    )

    As this uses just one foreach() which iterates for each field in a datatype it will be run for fixed times always and wont affect the performance adversely. Hope this helps!

  • 0
    Certified Lead Developer
    in reply to Harsha Sharma

    This one is interesting in how it avoids issues with invalid type comparisons by casting everything to text via the filter predicate rule. I'll have to keep it in mind for the future as it makes things much more resilient in most use cases. What's fun - I think there's a way to change the a!forEach() into a fn!reduce, to remove the "loop", but in reality that just masks the loop within a looping function.

    The only caution with this approach for a generic rule is when dealing with numbers that have leading or trailing zeros - for example, 0.20 should be considered equal to 0.2 in many finance contexts.

Reply
  • 0
    Certified Lead Developer
    in reply to Harsha Sharma

    This one is interesting in how it avoids issues with invalid type comparisons by casting everything to text via the filter predicate rule. I'll have to keep it in mind for the future as it makes things much more resilient in most use cases. What's fun - I think there's a way to change the a!forEach() into a fn!reduce, to remove the "loop", but in reality that just masks the loop within a looping function.

    The only caution with this approach for a generic rule is when dealing with numbers that have leading or trailing zeros - for example, 0.20 should be considered equal to 0.2 in many finance contexts.

Children
No Data