<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Merge two dictionaries into one in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/merge-two-dictionaries-into-one/m-p/1124141#M5456</link>
    <description>&lt;P&gt;Let's solve this with a custom function! For each key in the input dicts, we need to see if the key already exists. If it doesn't, add it as a new key to the output; if it does, add the values together.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var d1 = {"A":10, "B":20, "C":30}
var d2 = {"A":1, "B":14, "D":3, "E":5}

function dict_merge(dicts_array){
    // Empty dict for output
    var out_dict = {}
    
    // Loop over array of input dicts
    for(var dict in dicts_array){

        // Loop over keys in dict
        for(var d in dicts_array[dict]){

            // Add / sum dict value depending on presence of key in out_dict
            if(!HasKey(out_dict, d)){
                out_dict[d] = dicts_array[dict][d]
            } else {
                out_dict[d] += dicts_array[dict][d]
            }
        }
    }
    
    return out_dict
}

dict_merge([d1, d2])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_0-1639018458465.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/29201i02A4CFF02A3C6D9D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_0-1639018458465.png" alt="jcarlson_0-1639018458465.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The best part is, a custom function like this can be used anywhere else you might need it, and is written in such a way that any number of dicts can be supplied, and the output will still work.&lt;/P&gt;</description>
    <pubDate>Thu, 09 Dec 2021 02:56:17 GMT</pubDate>
    <dc:creator>jcarlson</dc:creator>
    <dc:date>2021-12-09T02:56:17Z</dc:date>
    <item>
      <title>Merge two dictionaries into one</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/merge-two-dictionaries-into-one/m-p/1124044#M5455</link>
      <description>&lt;P&gt;I have two dictionaries that I would like to merge into a combined one with the "values" added together (see below).&amp;nbsp; The dictionaries are the results of "Summary Counts" of two different columns of data in a hosted feature layer.&amp;nbsp; Here are the dictionaries&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;d1 = {"A":10, "B":20, "C":30}&lt;/P&gt;&lt;P&gt;d2 = {"A":1, "B":14, "D":3, "E":5}&lt;/P&gt;&lt;P&gt;I would like to end up with something like:&lt;/P&gt;&lt;P&gt;d3 =&amp;nbsp;{"A":11, "B":34, "C":30, "D":3, "E":3}&lt;/P&gt;&lt;P&gt;As you can see, the dictionary "keys" may not be the same between the two summaries every time.&amp;nbsp; Ultimately, I want to use the combined summary (i.e., the data in"d3") in a Serial Chart in a Dashboard.&amp;nbsp; I'm struggling with how to compare the two dictionaries and check for corresponding keys in each.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 22:07:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/merge-two-dictionaries-into-one/m-p/1124044#M5455</guid>
      <dc:creator>ToddLusk</dc:creator>
      <dc:date>2021-12-08T22:07:31Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two dictionaries into one</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/merge-two-dictionaries-into-one/m-p/1124141#M5456</link>
      <description>&lt;P&gt;Let's solve this with a custom function! For each key in the input dicts, we need to see if the key already exists. If it doesn't, add it as a new key to the output; if it does, add the values together.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var d1 = {"A":10, "B":20, "C":30}
var d2 = {"A":1, "B":14, "D":3, "E":5}

function dict_merge(dicts_array){
    // Empty dict for output
    var out_dict = {}
    
    // Loop over array of input dicts
    for(var dict in dicts_array){

        // Loop over keys in dict
        for(var d in dicts_array[dict]){

            // Add / sum dict value depending on presence of key in out_dict
            if(!HasKey(out_dict, d)){
                out_dict[d] = dicts_array[dict][d]
            } else {
                out_dict[d] += dicts_array[dict][d]
            }
        }
    }
    
    return out_dict
}

dict_merge([d1, d2])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_0-1639018458465.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/29201i02A4CFF02A3C6D9D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_0-1639018458465.png" alt="jcarlson_0-1639018458465.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The best part is, a custom function like this can be used anywhere else you might need it, and is written in such a way that any number of dicts can be supplied, and the output will still work.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Dec 2021 02:56:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/merge-two-dictionaries-into-one/m-p/1124141#M5456</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-12-09T02:56:17Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two dictionaries into one</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/merge-two-dictionaries-into-one/m-p/1124244#M5458</link>
      <description>&lt;P&gt;Awesome, thank you so much!&amp;nbsp; I had the "logic" right in my head but couldn't quite figure out the syntax.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Dec 2021 11:04:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/merge-two-dictionaries-into-one/m-p/1124244#M5458</guid>
      <dc:creator>ToddLusk</dc:creator>
      <dc:date>2021-12-09T11:04:55Z</dc:date>
    </item>
  </channel>
</rss>

