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.

5
  • 1
    convert it to json Commented Jul 6, 2017 at 8:12
  • You means that you can't split it on , then on = each result to get a key/value array ? That's a bit strange Commented Jul 6, 2017 at 8:13
  • @ScaryWombat Do we need to do that on the back end side? Having trouble with that on the front end because of the '=' Commented Jul 6, 2017 at 8:20
  • @AxelH Thanks, I tried the split but it breaks on the '='. Back end guys refusing to escape characters. Commented Jul 6, 2017 at 8:21
  • 1
    Do we need to do that on the back end side? that would be easiest I thiink Commented Jul 6, 2017 at 8:22

1 Answer 1

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

})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. Had to tweek it a TINY bit but it was exactly what I needed to finish it.
Glad to know that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.