Add open and close square brackets to a list in sling model | Community
Skip to main content
Level 3
Jun 21, 2022 9:41 AM UTC
Solved

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

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.
Tushar_GuptaAccepted solution
Employee
Jun 21, 2022 9:53 AM UTC

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_
Level 5
Jun 21, 2022 2:01 PM UTC

@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 "["

Translate
Varun_Shakya
New Participant
Jun 21, 2022 11:43 AM UTC

@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.

Translate
ShaileshBassi
New Participant
Jun 21, 2022 11:27 AM UTC

@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

Translate
sravs
New Participant
Jun 21, 2022 11:11 AM UTC

@keerthi97 ,

 

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

Translate
Tushar_GuptaAccepted solution
Employee
Jun 21, 2022 9:53 AM UTC

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...

Translate