Add open and close square brackets to a list in sling model | Community
Skip to main content
New Participant
June 21, 2022
Solved

Add open and close square brackets to a list in sling model

  • June 21, 2022
  • 5 replies
  • 1663 views

Hi Team ,

I need to enclose the arraylist within square bracket and so used list.add("[") at the start and at the end as list.add("]") but it appends comma with the list .But need to get rid off the comma from the list and so could anyone please suggest on this .

 

Actual:[, [1245,480], [, [350,280], [300,250], ], ], [, [800,480], [, [300,250], [728,90], ], ]
Expected:[ [1245,480], [[350,280], [300,250]]], [[800,480], [[300,250], [728,90]]]

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Tushar_Gupta

I don't understand the complexity & usecase. However, you can certainly use the conditions at the start of the loop where you are building an array and at the last to not include the ",". In sling model, you have the control and you can easily use the core java features & add some logic to meet your desired need.

 

  • First make sure there should not be any comma post [
  • Comma should not follow bracket closure

etc...

5 replies

sunil_kumar_
New Participant
June 21, 2022

@keerthi97 It will add. You are adding "[" as one item to list. What is your use and what are you trying to achieve. could you add some more details. That may be possible without adding "["

Varun_Shakya
New Participant
June 21, 2022

@keerthi97 

Why not storing your arrayList items in a map, and then iterate over the values of map in sightly . 

I think that would be easy.

ShaileshBassi
New Participant
June 21, 2022

@keerthi97 I would suggested to return the String/List whatever is the business requirement from the Sling Model once created and do the manipulation for appending the respective brackets i.e. opening "[" and closing "]" in your component's HTML.

 

<div data-sly-use.obj="com.mysite.core.models.MyClass">
[ ${obj.listString} ]
</div>

Thanks

sravs
New Participant
June 21, 2022

@keerthi97 ,

 

Try to use toString() method on list which will automatically add braces at start and end of the list.

Tushar_GuptaAccepted solution
Employee
June 21, 2022

I don't understand the complexity & usecase. However, you can certainly use the conditions at the start of the loop where you are building an array and at the last to not include the ",". In sling model, you have the control and you can easily use the core java features & add some logic to meet your desired need.

 

  • First make sure there should not be any comma post [
  • Comma should not follow bracket closure

etc...