The reason why it's not working for you is:
- You are appending to the
remainder, instead of the new array. And concat expects an array as parameter, which it will then combine with the other array
- As in your case the item to be appended using
concat is an array, you need to nest it inside another array.
I would also advise using the slice(0) method to duplicate the initial array, and concat a new array to it, which contains the new array:
# Duplicate an array of arrays, and append a new array to it, returning the new array.
# @params list [Array<Array>] array of arrays
# @params newItem [Array] new array
# @return [Array<Array>] duplicate of array with new array appended to it
append = (list, newItem) ->
list.slice(0).concat [newItem]
a = [[], [1,1], [2]]
append a, [3,3]
# => [[], [1,1], [2], [3,3]]
a
# => [[], [1,1], [2]]
Slice will make a copy of part of the array. slice(0) will start at the 0th index, and go to the end, effectively making a copy of the array.
Concat joins two arrays. But you want to keep the new array intact. Therefore you have to wrap it in another array which then gets merged into the main array.