Need to manipulate the array

Certified Associate Developer

local!data: {
{ name: "John", status: "Active", test: 10 },
{ name: "John", status: null, test: 4 },
{ name: "Alice", status: "Active", test: 5 },
{ name: "Alice", status: null, test: 6 },
{ name: "Bob", status: null, test: 8 }
}

This a the above data now my requirement is remove null status only for the duplicate name and transfer the test value to the non-null status.

Can any one please help me with this.

  Discussion posts and replies are publicly visible

Parents
  • I'm not sure how this use case has come about but it appears to me that something has gone wrong to require doing this manipulation. I don't feel good about the below solution because it relies on too many assumptions and just feels flimsy. Having said that, I hope the below helps.

    I think this is the output you are looking for:

    {
        { name: "John", status: "Active", test: 4},
        { name: "Alice", status: "Active", test: 6},
        { name: "Bob", status: null, test: 8}
    }

    The code:

    a!localVariables(
      local!data: {
        { name: "John", status: "Active", test: 10 },
        { name: "John", status: null, test: 4 },
        { name: "Alice", status: "Active", test: 5 },
        { name: "Alice", status: null, test: 6 },
        { name: "Bob", status: null, test: 8 }
      },
      a!forEach(
        items: local!data,
        expression: a!localVariables(
          local!name: index(
            fv!item,
            "name",
            {}
          ),
          local!status: index(
            fv!item,
            "status",
            {}
          ),
          local!indexesWhereNameMatches: where(
            local!name = index(
              local!data,
              "name",
              {}
            )
          ),
          local!hasDuplicate: length(local!indexesWhereNameMatches) > 1,
          /*Assume only one duplicate is possible*/
          local!duplicateIndex: if(
            local!hasDuplicate,
            difference(
              local!indexesWhereNameMatches,
              fv!index
            ),
            {}
          ),
          /*Assume duplicate will always have null status and therefore to use that test value*/
          local!testValueToUse: if(
            local!hasDuplicate,
            index(
              index(
                index(
                  local!data,
                  local!duplicateIndex,
                  {}
                ),
                "test",
                {}
              ),
              1,
              {}
            ),
            index(
              fv!item,
              "test",
              {}
            )
          ),
          local!statusIsNull: a!isNullOrEmpty(local!status),
          local!replaceTestValue: and(
            local!hasDuplicate,
            not(local!statusIsNull)
          ),
          local!removeIndex: and(
            local!statusIsNull,
            local!hasDuplicate
          ),
          local!allDetailsNeeded: a!map(
            name: local!name,
            status: local!status,
            hasDuplicate: local!hasDuplicate,
            statusIsNull: local!statusIsNull,
            replaceTestValue: local!replaceTestValue,
            testValueToUse: local!testValueToUse,
            removeIndex: local!removeIndex
          ),
          if(
            local!removeIndex,
            {},
            a!update(
              data: fv!item,
              index: "test",
              value: local!testValueToUse
            )
          )
        )
      )
    )

Reply
  • I'm not sure how this use case has come about but it appears to me that something has gone wrong to require doing this manipulation. I don't feel good about the below solution because it relies on too many assumptions and just feels flimsy. Having said that, I hope the below helps.

    I think this is the output you are looking for:

    {
        { name: "John", status: "Active", test: 4},
        { name: "Alice", status: "Active", test: 6},
        { name: "Bob", status: null, test: 8}
    }

    The code:

    a!localVariables(
      local!data: {
        { name: "John", status: "Active", test: 10 },
        { name: "John", status: null, test: 4 },
        { name: "Alice", status: "Active", test: 5 },
        { name: "Alice", status: null, test: 6 },
        { name: "Bob", status: null, test: 8 }
      },
      a!forEach(
        items: local!data,
        expression: a!localVariables(
          local!name: index(
            fv!item,
            "name",
            {}
          ),
          local!status: index(
            fv!item,
            "status",
            {}
          ),
          local!indexesWhereNameMatches: where(
            local!name = index(
              local!data,
              "name",
              {}
            )
          ),
          local!hasDuplicate: length(local!indexesWhereNameMatches) > 1,
          /*Assume only one duplicate is possible*/
          local!duplicateIndex: if(
            local!hasDuplicate,
            difference(
              local!indexesWhereNameMatches,
              fv!index
            ),
            {}
          ),
          /*Assume duplicate will always have null status and therefore to use that test value*/
          local!testValueToUse: if(
            local!hasDuplicate,
            index(
              index(
                index(
                  local!data,
                  local!duplicateIndex,
                  {}
                ),
                "test",
                {}
              ),
              1,
              {}
            ),
            index(
              fv!item,
              "test",
              {}
            )
          ),
          local!statusIsNull: a!isNullOrEmpty(local!status),
          local!replaceTestValue: and(
            local!hasDuplicate,
            not(local!statusIsNull)
          ),
          local!removeIndex: and(
            local!statusIsNull,
            local!hasDuplicate
          ),
          local!allDetailsNeeded: a!map(
            name: local!name,
            status: local!status,
            hasDuplicate: local!hasDuplicate,
            statusIsNull: local!statusIsNull,
            replaceTestValue: local!replaceTestValue,
            testValueToUse: local!testValueToUse,
            removeIndex: local!removeIndex
          ),
          if(
            local!removeIndex,
            {},
            a!update(
              data: fv!item,
              index: "test",
              value: local!testValueToUse
            )
          )
        )
      )
    )

Children
No Data