如何将以下 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} 就是一个例子。我想我的实际代码不会有什么不同。
How to iterate HashMap using JSTL forEach loop?
<c:forEach var="department" items="${departmentHash}">
Country: ${department.key} - Capital: ${department.value}
</c:forEach>
以上没有return任何东西进入${department}
其他 links 有类似的答案,我无法开始工作。
How to loop through a HashMap in JSP?
How to iterate an ArrayList inside a HashMap using JSTL?
我的问题措辞可能有误,所以如果有人对我有正确的 link 或对我的问题的回答,我们将不胜感激。
由于密钥部分中的整数(12、2、4 等),无法解析您提供的字符串。
如果您以字符串的形式获取 hashmap 数据,您可以在 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
})
长话短说。
我们的后端团队为我提供了一个 hashMap,它具有以下输出。
{12=其他服务(辅助),2=其他服务,4=收集,17=获取新服务(对我而言),19=获取新服务(对我的业务而言)}
我是一名前端开发人员,从未使用过这样的字符串。由于 '='
,我无法使用 jquery.split() 拆分它我在网上搜索了很多我的问题的答案,但无法正常工作,如果它实际上是我问题的正确答案的话。
以下是我试过的方法。 ${departmentHash} 就是一个例子。我想我的实际代码不会有什么不同。
How to iterate HashMap using JSTL forEach loop?
<c:forEach var="department" items="${departmentHash}">
Country: ${department.key} - Capital: ${department.value}
</c:forEach>
以上没有return任何东西进入${department}
其他 links 有类似的答案,我无法开始工作。
How to loop through a HashMap in JSP?
How to iterate an ArrayList inside a HashMap using JSTL?
我的问题措辞可能有误,所以如果有人对我有正确的 link 或对我的问题的回答,我们将不胜感激。
由于密钥部分中的整数(12、2、4 等),无法解析您提供的字符串。
如果您以字符串的形式获取 hashmap 数据,您可以在 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
})