You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An idea for a new package: An "upsert" function for arrays. This would be based on the "upsert" operation commonly used in databases, which is a combination of "update" and "insert", however, we would adapt it to fit better for use with arrays.
The idea is that you pass an array, and index that you want to "upsert" into, and the element which you want to be "upserted". If the provided index is within the bounds of the array, then the provided item will replace the item at that index. If the provided index is outside the bounds of the provided index, then it gets pushed onto the end of the array.
I can't really say for sure how much a function like this would be used/needed, I just thought I would suggest it since I had to write my own version of it for a recent project.
The text was updated successfully, but these errors were encountered:
Hi Thomas, thanks for the suggestion.
My first thought was you could achieve the same with Array#splice.
But that said, splice mutates and does not return the updated value. So yes I think this has value!
An idea for a new package: An "upsert" function for arrays. This would be based on the "upsert" operation commonly used in databases, which is a combination of "update" and "insert", however, we would adapt it to fit better for use with arrays.
You would call the function similar to this:
The idea is that you pass an array, and index that you want to "upsert" into, and the element which you want to be "upserted". If the provided index is within the bounds of the array, then the provided item will replace the item at that index. If the provided index is outside the bounds of the provided index, then it gets pushed onto the end of the array.
I can't really say for sure how much a function like this would be used/needed, I just thought I would suggest it since I had to write my own version of it for a recent project.
The text was updated successfully, but these errors were encountered: