To add items in Nunjucks, use the .push() function.
{% set arr = [1,2] %}
{% set arr = (arr.push(3), arr) %}
Enter fullscreen mode
...
For further actions, you may consider blocking this person and/or reporting abuse
Instead of
push
useconcat
, it will simplify things a bit:Thanks for sharing Felipe! I didn't know about the
concact()
method. I've just checked the official Mozilla blog, and they wrote only about join() for concatenate string. Thanks for make me discovering it!Thanx for inspiration!
I've ended up with the following:
Result in output:
I din't know the reject() method! Thanks for sharing Pavel!
Thank you! I had no idea that you can execute arbitrary JS right in the tags.
Do you know where I can read more about that? You’re not just reassigning the value with the pushed item, it’s also wrapped in brackets and you repeat array’s name after a comma. Where this comes from? 🤔
hey Vadim, thank you for your feedback! I just added at the end of the post where I found the
push
syntax 😊 I can't really tell why this filter is not available on the official docs.It is a great post. though I am stuck at a point where I need to pass this final array to another statement such as:
window.master.init({{domesticAnimals}});
. this showswindow.master.init(cat,dog);
instead of array. what should I do to pass an array in the above statement. I need data like `window.master.init(['cat','dog']).Exactly what I was looking for. Worked like a charm !
Thank you !
I'm glad it was helpful! 💪🏻 Thank you
This is very useful - thanks
I'm glad that it has been helpful to you. 😊
is there a similar filter to add additional key/value properties in an object?