Skip to main content
August 22, 2023
Question

retrieve key field in dictionary from value

  • August 22, 2023
  • 4 replies
  • 0 views

Hello everyone.

I want to retrieve the key field in the dictionary(queried from cdt) based on the value present.

I have the data set in following order.


txt1    112      (Number (Decimal))

txt2    113     (Number (Decimal))

txt3    114    (Number (Decimal))

txt4    115   (Number (Decimal))

Input: 115

expected output: txt4

Please help me in getting the answer.

    4 replies

    mathieud0001
    August 22, 2023

    a!localVariables(
      local!dictionary: { txt1: 112, txt2: 113, txt3: 114, txt4: 115 },
      reject(
        fn!isnull,
        a!forEach(
          items: a!keys(local!dictionary),
          expression: a!localVariables(
            local!value: todecimal(index(local!dictionary, fv!item, null)),
            if(local!value = ri!input, fv!item, null)
          )
        )
      )
    )

    August 22, 2023

    Hello Mathieu Drouin,

    Here dictionary is the output from the query entity.

    a!keys will through the following error.


    Expression evaluation error at function a!keys [line 44]: The passed parameter(s) are of the wrong type. Received the type List of Variant. 

    mathieud0001
    August 22, 2023

    Here's a version that takes the dictionary as an input and casts it to a map.

    reject(
      fn!isnull,
      a!forEach(
        items: a!keys(cast(typeof(a!map()), ri!dictionary)),
        expression: a!localVariables(
          local!value: todecimal(index(ri!dictionary, fv!item, null)),
          if(local!value = ri!input, fv!item, null)
        )
      )
    )