简体   繁体   English

一个如何转换以下hashMap <String, String> 到可用的JS对象?

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

Long story short. 长话短说。

I'm being given a hashMap from our back end team that has the following output. 我们的后端团队为我提供了一个hashMap,它具有以下输出。

{12=Other Services (Assisted), 2=Other Services, 4=Collect, 17=Get New (For Me), 19=Get New (For My Business)} {12 =其他服务(辅助),2 =其他服务,4 =收集,17 =获取新内容(对我来说),19 =获取新内容(对我来说业务)}

I'm a front end develop and have never worked with a string like this. 我是前端开发人员,从未使用过这样的字符串。 I'm unable to split it using jquery.split() because of the '=' 由于'=',我无法使用jquery.split()对其进行拆分

Searching online I found a lot of answers to my question but can't get it to work, if it is in fact the correct answer to my question. 在网上搜索时,我发现了很多问题的答案,但实际上却是我问题的正确答案,但无法解决。

Below is what I've tried. 以下是我尝试过的方法。 ${departmentHash} is an example. $ {departmentHash}是一个示例。 My actual code won't make a difference I suppose. 我想我的实际代码不会有所作为。

How to iterate HashMap using JSTL forEach loop? 如何使用JSTL forEach循环迭代HashMap?

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

The above didn't return anything in to ${department} 上面没有返回$ {department}中的任何内容

Other links had similar answers that I could not get to work. 其他链接有类似的答案,我无法上班。

How to loop through a HashMap in JSP? 如何在JSP中遍历HashMap?
How to iterate an ArrayList inside a HashMap using JSTL? 如何使用JSTL在HashMap中迭代ArrayList?

I might be wording my question wrong so if anyone has a correct link for me or an answer to my question it would be much appreciated. 我可能对我的问题的措辞有误,因此,如果有人对我有正确的链接或对我的问题的回答,将不胜感激。

Parsing the string that you have provided wont work because of integers(12,2,4, etc) in the key section. 由于键部分中的整数(12、2、4等),因此无法解析您提供的字符串。

If you are getting the hashmap data in the form of a string, the you could try something like the following in javascript: 如果您以字符串形式获取哈希图数据,则可以在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