
Line 10
When I’m looking at a function, I’d prefer not to also have to hold global state in my head – so I’m all for functional programming as far as that goes. I’m less concerned about side effects, so I wouldn’t always bother to copy a parameter like this, but the argument is stronger for an array than an object since in other languages an array might be a value type.
The copy itself is noteworthy since I’m using the cool [...x] spread syntax. This is one of the newish iterator tools which returns any iterate-able data as an array. Since this is right at the top of our function, we’re also indicating to the reader we’re expecting a one-dimensional array.
Line 11
array.sort() works as expected, and in place – so our new array is mutated – hence the copying earlier. It can optionally be passed in an arrow function to determine the sort test, but if omitted assumes ascending according to the < and > rules. The format of this function is a bit different that in Swift. For the standard sort it is array.sort((a, b) => (a - b)) whereas in Swift it would have been ( a > b ). This is because in JS the function result is compared against zero to see if the positions are swapped. This seems odd, but I’m sure there’s a reason.
Lines 13-20
The for (x of y) syntax is a neat iterator loop for when you need the item (but not index) of a collection. I slightly regret using number for the array element – since this is JS it could be number, string or anything.
We check the new object we created to see if it has a property with the name of the current element. For example if this element of our array is 254, we check to see if the object has a property of that name – eg numberObject.254. That’s the square brackets on the object. It’s a neat bit of meta that would be challenging in other languages.
If there’s no property with that name we add it as an empty array. The value from the array is appended to this array in the object. So if we had an array of [2, 2, 3, 4] we’d end up with an object.
2 - [2, 2]
3 - [3]
4 - [4]
Lines 22-28
Where there’s more than one of the same value (as in 2 above) we want an array, but if there’s only a single value, we want the raw value. So the next segment of code is to work through each property of the object and change any single values to just their values instead of a single element array. This is a great example of something that’s neat and clear in JS but would not be possible in a strictly typed language.
We use for x in y this time to inspect each property of an object (rather than elements in an array).
Line 31
values() is just a standard method that returns an array version of an object.
Leave a comment