Skip to main content
March 1, 2025
Question

how to split a map based on user input value

  • March 1, 2025
  • 4 replies
  • 0 views

hi, 

I am trying to split a map based on user input 

local!data: {
a!map(id: 1, name: "Alice", age: 10, city: "New York",school:"NewYork"),
a!map(id: 2, name: "Bob", age: 12, city: "Los Angeles",school:"NewYork"),
a!map(id: 3, name: "Charlie", age: 13, city: "Chicago",school:"NewYork"),
a!map(id: 4, name: "David", age: 14, city: "Houston",school:"NewYork")
},

if I give input : age 

I should get two 

local!data1:{

a!map(id: 1, name: "Alice", age: 10, ),
a!map(id: 2, name: "Bob", age: 12, ),
a!map(id: 3, name: "Charlie", age: 13, ),
a!map(id: 4, name: "David", age: 14, )}  and 

local!data2:{a!map(city: "New York",school:"NewYork"),

a!map(city: "Los Angeles",school:"NewYork"),
a!map(city: "Chicago",school:"NewYork"),
a!map(city: "Houston",school:"NewYork")}

is there a way to achieve this , I tried ldrop and rdrop, it doesnt give output as expected 

    4 replies

    karumurua531442
    March 1, 2025

    hi [mention:92ba06295fbc4d77b281c512999a858b:e9ed411860ed4f2ba0265705b8793d05]  May I know what your use case is and what you are trying to achieve 

    konduruc733182
    March 1, 2025

    Your requirement is unclear from here.

    I should get two 

    local!data1:{

    Are you expecting two different dictionaries based on your input? Also what criteria are you looking at for the split. 

    stefanhelzle0001
    March 1, 2025

    You can easily iterate on the input maps, and, based on conditions, create two new maps for each.

    venkatrea696188
    March 1, 2025

    I am not sure what you are trying to achieve but here is the sample code that matches your requirement

    a!localVariables(
      local!data: {
        a!map(
          id: 1,
          name: "Alice",
          age: 10,
          city: "New York",
          school: "NewYork"
        ),
        a!map(
          id: 2,
          name: "Bob",
          age: 12,
          city: "Los Angeles",
          school: "NewYork"
        ),
        a!map(
          id: 3,
          name: "Charlie",
          age: 13,
          city: "Chicago",
          school: "NewYork"
        ),
        a!map(
          id: 4,
          name: "David",
          age: 14,
          city: "Houston",
          school: "NewYork"
        )
      },
      local!keys: a!keys(local!data[1]),
      local!inputindex: if(
        a!isNullOrEmpty(ri!input),
        "",
        if(
          contains(local!keys, ri!input),
          wherecontains(touniformstring(ri!input), local!keys),
          null
        )
      ),
      local!keys2: if(
        a!isNullOrEmpty(local!inputindex),
        null,
        rdrop(
          local!keys,
          (length(local!keys) - local!inputindex)
        )
      ),
      local!keys3: if(
        a!isNullOrEmpty(local!inputindex),
        null,
        ldrop(local!keys, local!inputindex)
      ),
      local!data1: if(
        a!isNullOrEmpty(local!inputindex),
        null,
        cast(
          253,
          a!forEach(
            local!data,
            createdictionary(
              local!keys2,
              property(fv!item, local!keys2, null)
            )
          )
        )
      ),
      local!data2: if(
        a!isNullOrEmpty(local!inputindex),
        null,
        cast(
          253,
          a!forEach(
            local!data,
            createdictionary(
              local!keys3,
              property(fv!item, local!keys3, null)
            )
          )
        )
      ),
      {
        if(
          a!isNullOrEmpty(local!inputindex),
          "Enter right keyword",
          { local!data1, local!data2 }
        )
      }
    )