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
_.toggle function #1677
Comments
Hi @cojack! I think this is better suited as a mixin. |
Implemented nicely with function toggler(collection, item) {
const idx = _.indexOf(collection, item);
if(idx !== -1) {
collection.splice(idx, 1);
} else {
collection.push(item);
}
}
const model = [1, 2, 3];
const toggle = _.partial(toggler, model)
toggle(1); // [2,3]
toggle(4); // [2,3,4] |
I find it quite common use case. Would be useful without mixins :) |
One use case a handleChange = (e: Event) => {
const [...selectedOptions] = this.state.selectedOptions
if (selectedOptions.includes(selectedValue)) {
selectedOptions.splice(selectedOptions.indexOf(selectedValue), 1)
} else {
selectedOptions.push(selectedValue)
}
this.setState({
selectedOptions,
})
} could be written something like handleChange = (e: Event) => {
const [...selectedOptions] = this.state.selectedOptions
this.setState({
selectedOptions: _.toggle(e.target.value, selectedOptions)),
})
} |
_.xor([1], [2]);
= [1, 2]
_.xor([1, 2], [2]);
= [1] |
This comment has been minimized.
This comment has been minimized.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hello,
Could you implement an toggle function? By toggle I mean something like this:
Thanks in advance.
The text was updated successfully, but these errors were encountered: