how to use immutability-helper remove elements from array
See original GitHub issuevar state = [{name:'a'},{name:'b'},{name:'c'},{name:'d'}];
var indexToRemove = state.findIndex(item => item.name === 'c');
var newState = update(state, {$splice: [[indexToRemove, 1]]});
console.log(newState); // [{name: "a"}, {name: "b"}, {name: "d"}]```
if i have a list var list=['a','c'],
how to remove two elements from array 'state'
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Using immutability-helper with Array, Object, Map
1 Answer 1 · 1. Array of simple values · 2. Array of simple objects · 3. Array is in another object ·...
Read more >Mutate object or arrays without changing original source in ...
To use Immutability Helper you have to first install it by running the below command ... Remove array element example:.
Read more >Immutability Helpers - React
Available Commands · {$push: array} push() all the items in array on the target. · {$unshift: array} unshift() all the items in array...
Read more >immutability-helper - npm
Start using immutability-helper in your project by running `npm i immutability-helper`. ... Removing an element from an array.
Read more >[Solved]-splice in Immutability Helpers-Reactjs - appsloveworld
Coding example for the question splice in Immutability Helpers-Reactjs. ... Why not to use splice with spread operator to remove item from an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@joelcollinsdc my first instinct when reading your message was that perhaps it makes sense, and I was almost starting to make a PR to propose the new command, when I realized is not that simple.
$push
is a lot simpler, it always works by appending items at the end of the array. A potential$delete
action would raise some questions: would it let remove by index? Or by value? If it’s by value, would it remove all occurrences of that value in the array? I’m not saying it can’t be done, I’m just saying there’s no way to satisfy every different need, there are a few different ways in which to approach removing items from an array.Perhaps the simplest approach, without bloating the set of commands this library provide, is to use the
$apply
command to provide a function that specifically address what’s needed in each case, describing the state transformation one wants to achieve. And it becomes even simpler with the new shorthand notation for$apply
. The$splice
command is still useful in some cases of deletion as well:As you see none of these are that complex to merit its own command, and they’re all valid criteria for deleting, depending on the situation at hand. These differences make a generic
$delete
command difficult to incorporate.Finally, if you really need this command, in the form of one of the cases described above, several times in your app, you can always declare custom commands.
Would be nice to see any giant list of all sorts of examples. I’m always struggling with the library. Is there one