簡體   English   中英

一個如何轉換以下hashMap <String, String> 到可用的JS對象?

[英]How does one convert the following hashMap<String, String> to a usable JS object?

長話短說。

我們的后端團隊為我提供了一個hashMap,它具有以下輸出。

{12 =其他服務(輔助),2 =其他服務,4 =收集,17 =獲取新內容(對我來說),19 =獲取新內容(對我來說業務)}

我是前端開發人員,從未使用過這樣的字符串。 由於'=',我無法使用jquery.split()對其進行拆分

在網上搜索時,我發現了很多問題的答案,但實際上卻是我問題的正確答案,但無法解決。

以下是我嘗試過的方法。 $ {departmentHash}是一個示例。 我想我的實際代碼不會有所作為。

如何使用JSTL forEach循環迭代HashMap?

<c:forEach var="department" items="${departmentHash}">
    Country: ${department.key}  - Capital: ${department.value}
</c:forEach>

上面沒有返回$ {department}中的任何內容

其他鏈接有類似的答案,我無法上班。

如何在JSP中遍歷HashMap?
如何使用JSTL在HashMap中迭代ArrayList?

我可能對我的問題的措辭有誤,因此,如果有人對我有正確的鏈接或對我的問題的回答,將不勝感激。

由於鍵部分中的整數(12、2、4等),因此無法解析您提供的字符串。

如果您以字符串形式獲取哈希圖數據,則可以在javascript中嘗試以下操作:

var str = '{12=Other Services (Assisted), 2=Other Services, 4=Collect, 17=Get New (For Me), 19=Get New (For My Business)}';

str = str.replace('{','');
str = str.replace('}','');// these lines will remove the leading and trailing braces

var arr = str.split(','); // this will give you an array of strings with each index in the format "12=Other Services (Assisted)"

arr.forEach(function(item){
    // here you can split again with '=' and do what is required
    var s = item.split('=');
    var obj = {key: s[0], value:s[1]}; // this is up to your implementation

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM