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

    Assuming this is the output you were looking for:

    {
        "name": "John",
        "status": 10,
        "test": null
    },
    {
        "name": "Alice",
        "status": 5,
        "test": null
    },
    {
        "name": "Bob",
        "status": null,
        "test": 8
    }

    This will:

    • Remove any duplicate items with a status of null
    • Transfer the test value for any values that have a non-null status

    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 }
      },
      local!cleanData: a!forEach(
        items: local!data,
        expression: a!localVariables(
          local!dataWithoutCurrentItem: remove(local!data, fv!index),
          local!hasDuplicates: count(
            index(
              local!dataWithoutCurrentItem,
              wherecontains(
                fv!item.name,
                local!dataWithoutCurrentItem.name
              )
            )
          ) > 0,
          if(
            and(
              local!hasDuplicates,
              a!isNullOrEmpty(fv!item.status)
            ),
            null,
            if(
              a!isNotNullOrEmpty(fv!item.status),
              a!map(
                name: fv!item.name,
                status: fv!item.test,
                test: null
              ),
              fv!item
            )
          )
        )
      ),
      reject(a!isNullOrEmpty, local!cleanData)
    )

Reply
  • 0
    Certified Lead Developer

    Assuming this is the output you were looking for:

    {
        "name": "John",
        "status": 10,
        "test": null
    },
    {
        "name": "Alice",
        "status": 5,
        "test": null
    },
    {
        "name": "Bob",
        "status": null,
        "test": 8
    }

    This will:

    • Remove any duplicate items with a status of null
    • Transfer the test value for any values that have a non-null status

    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 }
      },
      local!cleanData: a!forEach(
        items: local!data,
        expression: a!localVariables(
          local!dataWithoutCurrentItem: remove(local!data, fv!index),
          local!hasDuplicates: count(
            index(
              local!dataWithoutCurrentItem,
              wherecontains(
                fv!item.name,
                local!dataWithoutCurrentItem.name
              )
            )
          ) > 0,
          if(
            and(
              local!hasDuplicates,
              a!isNullOrEmpty(fv!item.status)
            ),
            null,
            if(
              a!isNotNullOrEmpty(fv!item.status),
              a!map(
                name: fv!item.name,
                status: fv!item.test,
                test: null
              ),
              fv!item
            )
          )
        )
      ),
      reject(a!isNullOrEmpty, local!cleanData)
    )

Children
No Data