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!

Reply
  • 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!

Children
No Data