0

Long story short.

I'm being given a hashMap from our back end team that has the following output.

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

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 '='

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. My actual code won't make a difference I suppose.

How to iterate HashMap using JSTL forEach loop?

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

The above didn't return anything in to ${department}

Other links had similar answers that I could not get to work.

How to loop through a HashMap in JSP?
How to iterate an ArrayList inside a HashMap using JSTL?

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.

Gezzasa
  • 1,443
  • 8
  • 17

1 Answers1

1

Parsing the string that you have provided wont work because of integers(12,2,4, etc) in the key section.

If you are getting the hashmap data in the form of a string, the you could try something like the following in 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

})
AnkitMittal
  • 166
  • 2
  • 18