CMS Development

Sapna1
Participant

Remove a item from the list

SOLVE

Hello,

I want to remove a item from the any object list. Can it is possible in cms hubl.

0 Upvotes
1 Accepted solution
himanshurauthan
Solution
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Remove a item from the list

SOLVE

Hello @Sapna1 

Let's say you have a list of items called my_list, and you want to remove the item at index 2. Here's how you can do it:

{% set my_list = ['apple', 'banana', 'orange', 'grape'] %}

{% set index_to_remove = 2 %}

{% set my_list = my_list[:index_to_remove] + my_list[index_to_remove+1:] %}

{{ my_list }}

 

In this example, we first set the original list my_list to ['apple', 'banana', 'orange', 'grape']. Then, we set the index_to_remove variable to 2, indicating that we want to remove the item at index 2 (which is 'orange').

Finally, we reassign the my_list variable to a new list that consists of everything before the item we want to remove (my_list[:index_to_remove]), concatenated with everything after the item we want to remove (my_list[index_to_remove+1:]). This effectively removes the item at index 2 from the list.

You can adapt this code to work with any list of objects that you have in your CMS HubL template. Just replace the my_list variable with your own list variable, and set the index_to_remove variable to the index of the item you want to remove.

Thanks & Regards
Himanshu Rauthan

Digital Marketing & Inbound Expert In Growth Hacking Technology

View solution in original post

2 Replies 2
Indra
Guide | Elite Partner
Guide | Elite Partner

Remove a item from the list

SOLVE

@Sapna1,

 

What kind of list is it?

I have a examples using the rejactattr filter:

 

From a list of content (contents)

{% set removeItem = 'item-slug' %}
{% set newContents = contents|rejectattr("slug", "equalto", removeItem) %}

Indra Pinsel - Front-end developer - Bright Digital
Did my answer solve your issue? Help the community by marking it as the solution.
0 Upvotes
himanshurauthan
Solution
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Remove a item from the list

SOLVE

Hello @Sapna1 

Let's say you have a list of items called my_list, and you want to remove the item at index 2. Here's how you can do it:

{% set my_list = ['apple', 'banana', 'orange', 'grape'] %}

{% set index_to_remove = 2 %}

{% set my_list = my_list[:index_to_remove] + my_list[index_to_remove+1:] %}

{{ my_list }}

 

In this example, we first set the original list my_list to ['apple', 'banana', 'orange', 'grape']. Then, we set the index_to_remove variable to 2, indicating that we want to remove the item at index 2 (which is 'orange').

Finally, we reassign the my_list variable to a new list that consists of everything before the item we want to remove (my_list[:index_to_remove]), concatenated with everything after the item we want to remove (my_list[index_to_remove+1:]). This effectively removes the item at index 2 from the list.

You can adapt this code to work with any list of objects that you have in your CMS HubL template. Just replace the my_list variable with your own list variable, and set the index_to_remove variable to the index of the item you want to remove.

Thanks & Regards
Himanshu Rauthan

Digital Marketing & Inbound Expert In Growth Hacking Technology