繁体   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