<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Add elements in Array</title><link>https://community.appian.com/discussions/f/user-interface/39162/add-elements-in-array</link><description>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&amp;#39;t apply the addition directly . It should be in a loop. I</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148406?ContentTypeID=1</link><pubDate>Tue, 27 May 2025 06:56:41 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:8ae70101-2597-4aff-a64f-ad973bef6135</guid><dc:creator>rohithsas547494</dc:creator><description>&lt;p&gt;&lt;span data-teams="true"&gt;Hi &lt;a href="/members/srinivaasant574373"&gt;srinivaasant574373&lt;/a&gt;&amp;nbsp; This is the simplified version you can give a try,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;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)
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148400?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 14:42:52 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:0a393f31-2704-409e-968a-e55a26ca410e</guid><dc:creator>Stefan Helzle</dc:creator><description>&lt;p&gt;I use this complicated way to generate the local only to keep that nested list structure. You already have the data in that format.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148395?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 13:08:42 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:d455ff34-ceeb-477b-a9df-3bdcdd128599</guid><dc:creator>EREN_YEAGER</dc:creator><description>&lt;p&gt;As I understand it, there&amp;#39;s no need to follow the definition of &lt;code data-start="133" data-end="146"&gt;local!value&lt;/code&gt;. You can replace &lt;code data-start="164" data-end="177"&gt;local!value&lt;/code&gt; with your own variable&amp;mdash;the one on which you want to perform the calculation. You just need to copy this part accordingly. and i&lt;span&gt;t should work fine.&lt;br /&gt;&lt;/span&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!forEach(
    items: enumerate(count(a!flatten(local!value[1]))),
    expression: a!localVariables(
      local!outerIndex: fv!index,
      sum(
        a!forEach(
          items: enumerate(count(local!value)),
          expression: local!value[fv!index][local!outerIndex]
        )
      )
    )
  )&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148392?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 12:53:35 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:36275905-6c89-4833-a8b7-52b383c733f5</guid><dc:creator>srinivaasant574373</dc:creator><description>&lt;p&gt;Hi Stefen,&lt;/p&gt;
&lt;p&gt;The code is working for 2 elements. But in some scenarios I might have more than 2 elements based on length of array.&lt;/p&gt;
&lt;p&gt;In the below scenario I have 4 elements in total,&amp;nbsp; I have a local variable local!add containing all the elements.&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="/resized-image/__size/640x480/__key/communityserver-discussions-components-files/13/pastedimage1748263792072v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;I am trying the same code that you sent but the elements should not hardcoded.&lt;pre class="ui-code" data-mode="text"&gt;local!length:length(local!add)
local!ss:{local!add[1]},
  local!ee:difference(local!add,local!ss),
  
  
  local!value:a!forEach(
    items: enumerate(local!length),
    expression:
    if(fv!index = 1, {local!ss}, {local!ee})
  ),
  local!ff:a!forEach(
    items: enumerate(count(a!flatten(local!value[1]))),
    expression: a!localVariables(
      local!outerIndex: fv!index,
      sum(
        a!forEach(
          items: enumerate(count(local!value)),
          expression: local!value[fv!index][local!outerIndex]
        )
      )
    )
  ),&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148391?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 12:49:07 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:33b7a8d8-d1f1-439b-829a-032579fdea6b</guid><dc:creator>EREN_YEAGER</dc:creator><description>&lt;p&gt;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-&amp;gt;&amp;nbsp;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;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, &amp;quot;&amp;quot;),
  local!arr2: index(local!arr, 2, &amp;quot;&amp;quot;),
  /*addition*/
  local!arrayWithTheLessSize: if(
    length(local!arr1) &amp;lt;= length(local!arr2),
    local!arr1,
    local!arr2
  ),
  local!arrayWithTheMoreSize: if(
    length(local!arr1) &amp;gt; length(local!arr2),
    local!arr1,
    local!arr2
  ),
  a!forEach(
    items: local!arrayWithTheMoreSize,
    expression: fv!item + index(
      local!arrayWithTheLessSize,
      fv!index,
      0
    )
  )
)&lt;/pre&gt;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;one question, Can there be more than two items in that array?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148389?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 11:59:17 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:6ba54637-abf5-4ed9-b620-3cb130f11b69</guid><dc:creator>Harsha Sharma</dc:creator><description>&lt;p&gt;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.&amp;nbsp; Line 5 to 7 has actual solution.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;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])
)
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148388?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 11:47:59 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:6726362c-ee14-4940-aae2-e519bebf560d</guid><dc:creator>srinivaasant574373</dc:creator><description>&lt;p&gt;In my usecase I have both the arrays in a single local variable (local!final)&lt;/p&gt;
&lt;p&gt;Like this&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="/resized-image/__size/640x480/__key/communityserver-discussions-components-files/13/pastedimage1748259964180v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;I need to perform the addition by iterating this local variable.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148387?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 11:46:18 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:0cf83769-8f5a-4806-a2a8-495d236f6fd7</guid><dc:creator>Stefan Helzle</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;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]
        )
      )
    )
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148386?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 11:37:41 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:315f53d8-4365-4236-ac41-ba9a7baf8f6d</guid><dc:creator>kishorej1999</dc:creator><description>&lt;p&gt;Hii&amp;nbsp;&lt;a href="/members/srinivaasant574373"&gt;srinivaasant574373&lt;/a&gt;&amp;nbsp;,&lt;/p&gt;
&lt;p&gt;Here is the solution which can cover you use case,&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!a: { 0, 36, 2 },
  local!b: { 2, 82, 2 },
  local!arrayWithTheLessSize: if(
    length(local!a) &amp;lt;= length(local!b),
    local!a,
    local!b
  ),
  a!forEach(
    items: local!arrayWithTheLessSize,
    expression: fv!item + index(local!b, fv!index, null)
  )
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148385?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 11:28:54 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:b0c4a765-a568-4465-bd18-f63ea86608c1</guid><dc:creator>srinivaasant574373</dc:creator><description>&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="/resized-image/__size/640x480/__key/communityserver-discussions-components-files/13/pastedimage1748258669645v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;This output is stored in a local variable local!final&lt;/p&gt;
&lt;p&gt;I just want to continue from here. I have a local variable with 2 items and I need to add the elements inside the array . I want the output as 2,118,10&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148384?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 11:21:49 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:fe97ba17-5268-471b-bba1-76f270a4a245</guid><dc:creator>Stefan Helzle</dc:creator><description>&lt;p&gt;Code?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148383?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 11:16:10 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:81728d04-a35e-425e-92c6-0bcd9f1fb331</guid><dc:creator>srinivaasant574373</dc:creator><description>&lt;p&gt;I am trying a!foreach but can&amp;#39;t able to achieve the logic&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148382?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 11:14:20 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:56f94918-bf7d-4eda-8351-11fd7fab8115</guid><dc:creator>sureshs276515</dc:creator><description>&lt;p&gt;a!localVariables(&lt;br /&gt; local!first: { 1, 2, 3 },&lt;br /&gt; local!sen: { 4, 5, 6 },&lt;br /&gt; local!output: a!forEach(items: local!first, expression: fv!item) + a!forEach(items: local!sen, expression: fv!item),&lt;br /&gt; local!output&lt;br /&gt;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add elements in Array</title><link>https://community.appian.com/thread/148381?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 11:13:29 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:be33d7c2-3e50-46d4-8b44-0e21686d4c81</guid><dc:creator>Stefan Helzle</dc:creator><description>&lt;p&gt;Did you try a!foreach()?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>