Skip to main content
February 16, 2022
Solved

Match Index Result for String

  • February 16, 2022
  • 11 replies
  • 1 view

Hello,

I have a similar question to: https://community.appian.com/discussions/f/rules/21530/getting-one-property-value-from-dictionary-based-upon-another-property-value.

But instead of returning true/false, I need it to match it against a string value on match condition:

a!localVariables(
  local!myDictionary: {
    {
      item: 1,
      active: "A",
      description: "Widget",
      color: "Green"},
      {
        item: 2,
        active:"B",
        description: "another",
        color: "red"}
  },
  local!tempResults: if(fn!index(local!myDictionary,"active","") = "A" ,"yes","no"),
  reject(fn!isnull, local!tempResults)
)

The index returns the "A", but when I do a condition against it (if statement), nothing returns. I want the string "Green" returned if it matches "A". IT says it doesn't match it.

Best answer by stefanhelzle0001

While people seem to love solving any challenge using a!foreach, most of the time this is not necessary.

a!localVariables(
  local!myDictionary: {
    {
      item: 1,
      active: "A",
      description: "Widget",
      color: "Green"
    },
    {
      item: 2,
      active: "B",
      description: "Another",
      color: "red"
    },
    {
      item: 3,
      active: "A",
      description: "Blah",
      color: "Blue"
    }
  },
  index(
    local!myDictionary.color,
    wherecontains("A", touniformstring(local!myDictionary.active)),
    {}
  )
)

11 replies

saravanarajanj
February 16, 2022

Hi, the fn!index() returns an array. so we can use contains() operator instead of =

alexl0007Author
February 16, 2022

do you mean :

local!tempResults: if(contains(fn!index(local!myDictionary,"active",""),"A") ,"yes","no"),

andrewh0007
February 16, 2022

The way I'd approach this is to cycle through the local!myDictionary variable checking for active = "A" and if it does return the color index. See code below.

There are many ways to achieve a similar outcome though so depending on your exact use case a different approach may be better/more efficient. This is just one way.

a!localVariables(
  local!myDictionary: {
    {
      item: 1,
      active: "A",
      description: "Widget",
      color: "Green"
    },
    {
      item: 2,
      active: "B",
      description: "another",
      color: "red"
    }
  },
  a!forEach(
    items: local!myDictionary,
    expression: if(
      exact(
        tostring(
          index(
            fv!item,
            "active",
            {}
          )
        ),
        "A"
      ),
      index(
        fv!item,
        "color",
        {}
      ),
      {}
    )
  )
)

alexl0007Author
February 16, 2022

This works, but I figured there would be a simpler approach with abuilt-in function. But this is acceptable. Thank you. 

harshitb6843
February 16, 2022

Hi there,

This can be effectively achieved using displayValue function(). Try the below piece of code. 

a!localVariables(
  local!myDictionary: {
    {
      item: 1,
      active: "A",
      description: "Widget",
      color: "Green"},
      {
        item: 2,
        active:"B",
        description: "another",
        color: "red"}
  },
  displayvalue(
    "A",
    local!myDictionary.active,
    local!myDictionary.color,
    ""
  )
)

andrewh0007
February 16, 2022

I really like that! I've never used displayvalue() but it's clearly helpful.

It does appear to only work for the first match though so I've come up with the following. Is there a better way if the use case requires more than one match?

a!localVariables(
  local!myDictionary: {
    {
      item: 1,
      active: "A",
      description: "Widget",
      color: "Green"
    },
    {
      item: 2,
      active: "B",
      description: "Another",
      color: "red"
    },
    {
      item: 3,
      active: "A",
      description: "Blah",
      color: "Blue"
    }
  },
  reject(
    fn!isnull,
    a!forEach(
      items: local!myDictionary,
      expression: displayvalue(
        "A",
        {fv!item.active},
        fv!item.color,
        null
      )
    )
  )
)

stefanhelzle0001
Brainy
February 16, 2022

While people seem to love solving any challenge using a!foreach, most of the time this is not necessary.

a!localVariables(
  local!myDictionary: {
    {
      item: 1,
      active: "A",
      description: "Widget",
      color: "Green"
    },
    {
      item: 2,
      active: "B",
      description: "Another",
      color: "red"
    },
    {
      item: 3,
      active: "A",
      description: "Blah",
      color: "Blue"
    }
  },
  index(
    local!myDictionary.color,
    wherecontains("A", touniformstring(local!myDictionary.active)),
    {}
  )
)