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 modify an immutable array?

See original GitHub issue

When using seamless-immutable, what is the recommended way of deriving new values from existing one (when map and merge aren’t enough)? Typically, immutable data structures (IDS) have functions that create new IDS derived from them, such as Immutable.js’ aList.push(1, 2) or Clojure’s updateIn, assocIn, dissoc, etc. In seamless-immutable all array/object mutating functions just throw an error so to create a new IDS I suppose I have to do something like

var newArray; var tmp = Immutable([1,2,3]).asMutable(); tmp.push(4); newArray = Immutable(tmp);

Is that correct? Thank you!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
rtfeldmancommented, Jun 5, 2015

Keep in mind that the function map accepts does receive index, so you can just do:

var indexToUpdate = 2, newVal = "THREE";

Immutable(["one", "two", "three", "four"]).map(function(val, index) {
  return (index === indexToUpdate) ? newVal : val;
});

Admittedly more verbose than mutableArray[indexToUpdate] = newVal, but in my experience this hasn’t come up all that often.

6reactions
bradwestfallcommented, Apr 8, 2016

oh, duh. Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Update one of the objects in array, in an immutable way
You can use map to iterate the data and check for the fieldName, if fieldName is cityId then you need to change the...
Read more >
Immutable Update Patterns - Redux
Simplifying Immutable Updates with Redux Toolkit​​​ Our Redux Toolkit package includes a createReducer utility that uses Immer internally. ...
Read more >
All about Immutable Arrays and Objects in JavaScript
Immutable array operations. Push; Unshift; Pop; Shift; Removal and inserting of items; Sort and reverse. Immutable object operations. Modify ...
Read more >
Update arrays without mutating the original
Update arrays without mutating the original. ... The concept of not changing values is known as immutability.
Read more >
Immutable update patterns - Medium
Examples of native JavaScript values that are mutable include objects, arrays, functions, classes, sets, and maps. 2. An immutable object is ...
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