Skip to main content
February 7, 2025
Question

Need to manipulate the array

  • February 7, 2025
  • 3 replies
  • 0 views

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.

3 replies

jayaprakashr0001
February 7, 2025

hi,

So you want to remove the duplicate null with null status right. Hope the code will help

a!localVariables(
  local!data: {
    { name: "John", status: "Active", test: 10 },
    { name: "John", status: null, test: 4 },
    { name: "Kiran",status: null, test: 10},
    { name: "Kiran",status: "Active", test: 10},
    { name: "Alice", status: "Active", test: 5 },
    { name: "Alice", status: null, test: 6 },
    { name: "Bob", status: null, test: 8 }
  },
  local!uniqueNames:union(index(local!data,"name",null),index(local!data,"name",null)),
  local!DuplicateUsers:reject(fn!isnull,a!forEach(
    items: local!uniqueNames,
    expression: if(
      count(wherecontains(touniformstring(fv!item),touniformstring(index(local!data,"name",null))))>1,
      fv!item,
      null()
    )
  )),
  local!duplicateUserIndexes:index(local!data,wherecontains(
    touniformstring(local!DuplicateUsers),
    touniformstring(index(local!data,"name",null))
  ),null),
  reject(fn!isnull,a!forEach(
    items: local!duplicateUserIndexes,
    expression: if(
      touniformstring(index(fv!item,"status",null))=touniformstring(null),
      null,
      fv!item
    )
  ))
)

andrewh6762
February 7, 2025

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
        )
      )
    )
  )
)

mathieud0001
February 7, 2025

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)
)