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
  • +1
    Certified Lead Developer

    Without Loop is not possible.
    No built-in way to dynamically index multiple properties without iteration.

    a!localVariables(
      local!employee1: {
        name: "John",
        age: 30,
        dept: "IT",
        salary: 50000
      },
      local!employee2: {
        name: "John",
        age: 35,
        dept: "IT", 
        salary: 55000
      },
    
      /* Get all property names */
      local!allKeys: a!keys(local!employee1),
    
      /* Check which properties are different */
      local!areEqual: a!forEach(
        local!allKeys,
        index(local!employee1, fv!item) = index(local!employee2, fv!item)
      ),
    
      /* Find properties with different values */
      local!differentProps: index(
        local!allKeys,
        wherecontains(false, local!areEqual),
        null
      ),
    
      /* Show results */
      {
        differentProperties: local!differentProps,
    
        oldValues: a!forEach(
          local!differentProps,
          index(local!employee1, fv!item, null)
        ),
        /* Output: {30, 50000} */
    
        newValues: a!forEach(
          local!differentProps,
          index(local!employee2, fv!item, null)
        )
        /* Output: {35, 55000} */
      }
    )

  • Ok I got it, but what if my data structure is a nested structure? like this. Can I call this method recursively like in Java?

     local!employee1: {
        name: "John",
        age: 30,
        dept: {
            name: "IT",
            field2: xx,
            field3: xx
        },
        salary: 50000
      }

  • 0
    Certified Lead Developer
    in reply to Peon

    Doesn't support recursive function calls(you can simulate it by having an expression rule call itself conditionally to compare nested maps, checking property types and combining differences from each level)
    For nested structures:
    Compare each level separately (manual nesting)
    Flatten structure first, then compare
    JSON comparison -> a!toJson(obj1) = a!toJson(obj2) (only tells If different, not what)

    Best approach as per my understanding, Handle each nesting level explicitly with separate comparison logic.

Reply
  • 0
    Certified Lead Developer
    in reply to Peon

    Doesn't support recursive function calls(you can simulate it by having an expression rule call itself conditionally to compare nested maps, checking property types and combining differences from each level)
    For nested structures:
    Compare each level separately (manual nesting)
    Flatten structure first, then compare
    JSON comparison -> a!toJson(obj1) = a!toJson(obj2) (only tells If different, not what)

    Best approach as per my understanding, Handle each nesting level explicitly with separate comparison logic.

Children