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

    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} */
      }
    )

Reply
  • 0
    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} */
      }
    )

Children
No Data