Skip to main content
May 26, 2025
Solved

Add elements in Array

  • May 26, 2025
  • 14 replies
  • 0 views

Hi,

I have a local variable with 2 items .

I need to add each element in the array with the corresponding index and display the total . Eg {1,2,3} +{1,2,3} = {2,4,6}

But in my case I can't apply the addition directly . It should be in a loop. I even tried using merge function but it is not working.. . How to achieve this. The below output is present inside a local variable (local!final).I need to add {0+2,36+82,2+8} and form a single array.

    Best answer by stefanhelzle0001

    a!localVariables(
      local!values: a!forEach(
        items: enumerate(2),
        expression: if(fv!index = 1, {0,36,2}, {2,82,8})
      ),
      a!forEach(
        items: enumerate(count(a!flatten(local!values[1]))),
        expression: a!localVariables(
          local!outerIndex: fv!index,
          sum(
            a!forEach(
              items: enumerate(count(local!values)),
              expression: local!values[fv!index][local!outerIndex]
            )
          )
        )
      )
    )

    14 replies

    stefanhelzle0001
    May 26, 2025

    Did you try a!foreach()?

    May 26, 2025

    I am trying a!foreach but can't able to achieve the logic

    stefanhelzle0001
    May 26, 2025

    Code?

    May 26, 2025

    a!localVariables(
    local!first: { 1, 2, 3 },
    local!sen: { 4, 5, 6 },
    local!output: a!forEach(items: local!first, expression: fv!item) + a!forEach(items: local!sen, expression: fv!item),
    local!output
    )

    kishorej1999
    May 26, 2025

    Hii [mention:2636807152274a4380d4056f6dc9e842:e9ed411860ed4f2ba0265705b8793d05] ,

    Here is the solution which can cover you use case,

    a!localVariables(
      local!a: { 0, 36, 2 },
      local!b: { 2, 82, 2 },
      local!arrayWithTheLessSize: if(
        length(local!a) <= length(local!b),
        local!a,
        local!b
      ),
      a!forEach(
        items: local!arrayWithTheLessSize,
        expression: fv!item + index(local!b, fv!index, null)
      )
    )

    Thanks

    May 26, 2025

    In my usecase I have both the arrays in a single local variable (local!final)

    Like this

    I need to perform the addition by iterating this local variable. 

    harshas2775
    May 26, 2025

    You can try something like this! In your case the output is in local!a in the sample code below. you can ignore the other two variables local!x and local!y.  Line 5 to 7 has actual solution. 

    a!localVariables(
    local!x: { 0, 36, 2 },
    local!y: { 2, 82, 8 },
    local!a: a!foreach({ 1, 2 }, if(fv!item = 1, local!x, local!y)),
    a!foreach(
    local!a[1],
    sum(fv!item, local!a[2][fv!index])
    )
    )

    amaans4178
    May 26, 2025

    Hi You can index each items in a local variable and perform addition in a!foreach. For example if you have two items you can follow this code-> 

    a!localVariables(
      /*ignore below code (replicating ur array)*/
      local!a: { 0, 2,  },
      local!b: { 36, 82 },
      local!c: { 2, 8 },
      local!arr: merge(local!a, local!b, local!c),
      /*ignore above*/
      /*code for getting value in seprate varables*/
      local!arr1: index(local!arr, 1, ""),
      local!arr2: index(local!arr, 2, ""),
      /*addition*/
      local!arrayWithTheLessSize: if(
        length(local!arr1) <= length(local!arr2),
        local!arr1,
        local!arr2
      ),
      local!arrayWithTheMoreSize: if(
        length(local!arr1) > length(local!arr2),
        local!arr1,
        local!arr2
      ),
      a!forEach(
        items: local!arrayWithTheMoreSize,
        expression: fv!item + index(
          local!arrayWithTheLessSize,
          fv!index,
          0
        )
      )
    )
     

    one question, Can there be more than two items in that array? 

    May 27, 2025

    Hi [mention:2636807152274a4380d4056f6dc9e842:e9ed411860ed4f2ba0265705b8793d05]  This is the simplified version you can give a try,

    a!localVariables(
      local!array1: {1,2,3,4},
      local!array2: {1,2,3},
      local!array3: {1,2},
      a!forEach(
        merge(local!array1,local!array2,local!array3),
        sum(fv!item)
      )
    )