Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoffeeScript - remove an item once pushed

Tags:

coffeescript

How can I remove an element from the array which was pushed in with push?

animals = []

class Cat

cat = new Cat

animals.push cat

now can I say something like

animals.pull cat

to take the cat away from the array? Presuming that the cat variable is the same one that was pushed inside. I am just trying to create a kind of a dynamic collection..

like image 805
noncom Avatar asked Jun 19 '26 22:06

noncom


1 Answers

index = animals.indexOf cat
animals.splice index, 1 if index isnt -1

Remember, a CoffeeScript array is just a JavaScript array, so you can look at any appropriate documentation.

like image 62
James McLaughlin Avatar answered Jun 27 '26 22:06

James McLaughlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!