Add elements in Array

Certified Senior Developer

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.

  Discussion posts and replies are publicly visible

Parents
  • 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? 

Reply
  • 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? 

Children
No Data