How to find the first target element in an array

Hi all dears,

currently i need to find a specific fruit and give it a value, however the fruit name in the array may be duplicate and i need to give value only for the first one appeared the array:

For example:

Rule Inputs:

fruits(5 items)

[1] [fruitName:apple, price:""]

[2] [fruitName:banana, price:""]

[3] [fruitName:banana, price:""]

[4] [fruitName:peach, price:""]

[5] [fruitName:peach, price:""]

 

These data will be shown in a grid in interface, and i need to give the first banana([2]) 's price a value, but leave [3] banana's price as empty. 

should be like this:

[1] [fruitName:apple, price:""]

[2] [fruitName:banana, price:"100"]

[3] [fruitName:banana, price:""]

[4] [fruitName:peach, price:""]

[5] [fruitName:peach, price:""]

i created  local!fruitFlag = "banana" to map the fruit name and now i can give both banana's price value by using forEach(). so how can i only give the first one value?

millions thanks for help!

  Discussion posts and replies are publicly visible

  • Hi ,
    I think this code snippet will work for you. However I am confused about the scenario where you only have to assign one value. You can give the below code a try and see if it resolves your problem.

    a!localVariables(
      local!fruits: {
        {
          fruitName: "apple",
          price: ""
        },
        {
          fruitName: "banana",
          price: ""
        },
        {
          fruitName: "banana",
          price: ""
        },
        {
          fruitName: "peach",
          price: ""
        },
        {
          fruitName: "peach",
          price: ""
        }
      },
      local!fruitFlag: "banana",
      local!fruitIndex: index(wherecontains(local!fruitFlag,touniformstring(property(local!fruits,"fruitName"))),1,{}),
      a!forEach(
        items: local!fruits,
        expression: {
          fruitName: fv!item.fruitName,
          price: if(
            and(
              fv!item.fruitName=local!fruitFlag,
              fv!index=local!fruitIndex
            ),
            100,
            fv!item.price
          )
        }
      )
    )

    Thanks,
    -Harshit 

  • Hi Harshit,

    Thanks for your great help and your solution works very well now.

    I just take the fruit as an simple to demonstrate the issue i'm facing, the real scenario is more complex than this, sorry for you confuse and thanks again!