question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to use immutability-helper remove elements from array

See original GitHub issue
var 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:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

35reactions
gnapsecommented, Jan 11, 2018

@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:

// Delete a value (7) if found, all occurrences
update(state, {
  items: arr => arr.filter(item => item != 7),
})

// Delete a value (7) if found, first occurrence only
const index = state.items.indexOf(7);
if (index >= 0) {
  update(state, { items: { $splice: [[index, 1]] } });
}

// Delete at a specific index, no matter what value is in it
update(state, { items: { $splice: [[index, 1]] } });

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.

6reactions
GaddMastercommented, Jun 8, 2019

Would be nice to see any giant list of all sorts of examples. I’m always struggling with the library. Is there one

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found