Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Avatar

Level 3

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

 

1 Accepted Solution

Avatar

Correct answer by
Level 6

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

View solution in original post

5 Replies

Avatar

Correct answer by
Level 6

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

Avatar

Level 3

@Keerthi97 ,

 

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

Avatar

Level 8

@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

Avatar

Community Advisor

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

Avatar

Community Advisor

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