遍历 JavaScript 中的带有嵌套 HashMap 值的 HashMap,使用 Thymeleaf:

huangapple go评论63阅读模式
英文:

Iterate a HashMap with nested HashMap values in javascript by using thymeleaf

问题

这是添加到模型中的对象:

HashMap<String,HashMap<String,Integer>> eventByMonthAndYear;

我尝试的迭代方式

我尝试了以下方法,但目前都没有成功。

使用 Thymeleaf 迭代 HashMap

我的努力

<script type="text/javascript" th:inline="javascript">

/*<![CDATA[*/

   /*[# th:each="entry: ${eventByMonthAndYear}"]*/

	console.log(/*[[${entry}]]*/);
	
	/*[/]*/     
    
/*]]>*/

</script>

没有输出任何对象,请问如何至少检索第一个 HashMap?

英文:

This is the object added to the model:

HashMap&lt;String,HashMap&lt;String,Integer&gt;&gt; eventByMonthAndYear;

How I tried to iterate it

I've tried these solutions but none has worked so far for me.

Iterate HashMap using thymeleaf

My efford

&lt;script  type=&quot;text/javascript&quot; th:inline=&quot;javascript&quot;&gt;


/*&lt;![CDATA[*/

   /*[# th:each=&quot;entry: ${eventByMonthAndYear}&quot;]*/

	console.log(/*[[${entry}]]*/);
	
	/*[/]*/     
    
/*]]&gt;*/

&lt;/script&gt;

No object is printed, how can i at-least retrieve the first HashMap?

答案1

得分: 1

假设我们有以下的测试数据:

Map<String, Integer> intMapA = new HashMap();
intMapA.put("one", 1);
intMapA.put("two", 2);
intMapA.put("three", 3);
        
Map<String, Integer> intMapB = new HashMap();
intMapB.put("four", 4);
intMapB.put("five", 5);
intMapB.put("six", 6);
        
Map<String, Map<String, Integer>> eventByMonthAndYear = new HashMap();
eventByMonthAndYear.put("Event A", intMapA);
eventByMonthAndYear.put("Event B", intMapB);
       
Map<String, Object> model = new HashMap();
model.put("events", eventByMonthAndYear);

这些内容会传递到 Thymeleaf 模板中。

我们可以在 JavaScript 中如下访问 events 对象:

<script th:inline="javascript">
    var events = [[${events}]];
    console.log(events);
</script>

你可以按照通常的 JS 方式来迭代这个变量。

更有用的是在 HTML 中访问对象,并且使用 Thymeleaf 迭代嵌套的映射:

<body>
    <div th:each="event : ${events}">
        <div th:text="${event.key}">
        </div>
        <div th:each="eventItem : ${event.value}">
            <div th:text="${' -> ' + eventItem.key} + ' - ' + ${eventItem.value}"></div>
        </div>
    </div>
</body>

最后这个例子会在浏览器中显示如下内容:

Event B
-> six - 6
-> four - 4
-> five - 5
Event A
-> one - 1
-> two - 2
-> three - 3

显然,由于我们特定使用了哈希映射,检索的顺序不能保证与插入的顺序相同。

英文:

Assume we have the following test data:

Map&lt;String, Integer&gt; intMapA = new HashMap();
intMapA.put(&quot;one&quot;, 1);
intMapA.put(&quot;two&quot;, 2);
intMapA.put(&quot;three&quot;, 3);
        
Map&lt;String, Integer&gt; intMapB = new HashMap();
intMapB.put(&quot;four&quot;, 4);
intMapB.put(&quot;five&quot;, 5);
intMapB.put(&quot;six&quot;, 6);
        
Map&lt;String, Map&lt;String, Integer&gt;&gt; eventByMonthAndYear = new HashMap();
eventByMonthAndYear.put(&quot;Event A&quot;, intMapA);
eventByMonthAndYear.put(&quot;Event B&quot;, intMapB);
       
Map&lt;String, Object&gt; model = new HashMap();
model.put(&quot;events&quot;, eventByMonthAndYear);

This is what gets passed to the Thymeleaf template.

We can access the events object in Javascript as follows:

&lt;script th:inline=&quot;javascript&quot;&gt;
    var events = [[${events}]];
    console.log(events);
&lt;/script&gt;

You can iterate the variable in the usual JS way.

What may be of more use is to access the object in the HTML and iterate over the nested maps using Thymeleaf:

    &lt;body&gt;
        &lt;div th:each=&quot;event : ${events}&quot;&gt;
            &lt;div th:text=&quot;${event.key}&quot;&gt;
            &lt;/div&gt;
            &lt;div th:each=&quot;eventItem : ${event.value}&quot;&gt;
                &lt;div th:text=&quot;${&#39; -&gt; &#39; + eventItem.key} + &#39; - &#39; + ${eventItem.value}&quot;&gt;&lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/body&gt;

This last example displays the following in the browser:

Event B
-&gt; six - 6
-&gt; four - 4
-&gt; five - 5
Event A
-&gt; one - 1
-&gt; two - 2
-&gt; three - 3

Obviously, because of our specific use of hashmaps, the order of retrieval is not guaranteed to be the same as the insertion order.

huangapple
  • 本文由 发表于 2020年8月24日 00:15:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63549255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定