-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .push for pushing to arrays (and .slice, splice etc?) #1
Comments
Don't think you need pushing or slice/splicing for this library at all. It's pretty easy to use, and using push within a reducer is mutating state iirc. |
I think that idea was to expose an immutable const obj = { names: ['Alice'] }
dotProp.push(obj, 'names', 'Bob')
// => { names: ['Alice', 'Bob'] } |
FYI you can do this in two lines: function push (object, path, value) {
const array = dotProp.get(object, path)
return dotProp.set(object, path, array.concat(value))
} Push would be convenient, though. |
Just a small nit, your function will concatenate the value if it is an array, so you can't use it to push arrays to arrays... could be solved like this: function push (object, path, value) {
const array = dotProp.get(object, path)
return dotProp.set(object, path, [...array, value])
} |
@LinusU actually, > [1,2,3].concat([4,5],6)
[ 1, 2, 3, 4, 5, 6 ] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat |
What I meant is that let a = []
a.push([1, 2])
a.push([3, 4])
console.log(a)
// => [[1, 2], [3, 4]] let a = []
a = a.concat([1, 2])
a = a.concat([3, 4])
console.log(a)
// => [1, 2, 3, 4] If the function is named |
Aha, of course. I see what you mean. |
Unshift would be welcome too |
No description provided.
The text was updated successfully, but these errors were encountered: