How to manipulate immutably and mutably JavaScript Array with only ES6+

How to manipulate immutably and mutably JavaScript Array with only ES6+

JavaScript is not pure functional programming so some method has a side effect.

When I started to learn JavaScript’s methods, every time I confused about which method i…


This content originally appeared on DEV Community and was authored by Nash

How to manipulate immutably and mutably JavaScript Array with only ES6+

JavaScript is not pure functional programming so some method has a side effect.

When I started to learn JavaScript's methods, every time I confused about which method is immutable/mutable or what is a return value.

Especially, we developers might frequently use Array's methods. So I have always wanted to organize basic Array's methods of how to manipulate mutably and immutably without any library, using pure JavaScript.

Point

Basically, some of JavaScript Array's methods are mutable so a key is a spread operator. We can use a mutable JavaScript method immutably in spite of the mutable as long as we use a spread operator well.

I believe this is a better way as much as I know from a perspective of simplicity more than another.

Methods

Here is a summary table.

Action Mutable Immutable
#pop pop(): popped list.slice(-1)
#push push(...arg): lengthNum [...list, ...items]
#shift shift(): shifted [item, ...rest] = list
#unshift unshift( ...arg ): lengthNum newList = [...items, ...list]
#reverse reverse(): reversed newList = [...list].reverse()
#sort sort(): sorted [...list].sort()
#splice / slice splice( startIdx, deleteCount = 1 ) :listFromStartToEnd slice(startIdx, endIdx?)

Check one by one.

pop

Mutable: pop(): item

list = ['a', 'b', 'c']
item = list.pop()
// list: ['a', 'b'], item: 'c'
Enter fullscreen mode Exit fullscreen mode

Immutable

list = ['a', 'b', 'c']
[item] = list.slice(-1)
// item: 'c'
Enter fullscreen mode Exit fullscreen mode

push

Mutable: push(...arg): lengthNum

list = ['a', 'b', 'c']
length = list.push('d', 'e')
// list: ['a', 'b', 'c', 'd', 'e'], length: 5
Enter fullscreen mode Exit fullscreen mode

Immutable

list = ['a', 'b', 'c']
newList = [...list, 'd', 'e']
// newList: ['a', 'b', 'c', 'd', 'e']
Enter fullscreen mode Exit fullscreen mode

shift

Mutable: shift(): item

list = ['a', 'b', 'c']
item = list.shift()
// list: ['b', 'c'], item: 'a'
Enter fullscreen mode Exit fullscreen mode

Immutable

list = ['a', 'b', 'c']
[item, ...rest] = list
// item: 'a', rest: ['b', 'c']
Enter fullscreen mode Exit fullscreen mode

unshift

Mutable: unshift( ...arg ) :lengthNum

list = ['a', 'b', 'c']
length = list.unshift('x')
// list: ['x', 'a', 'b', 'c'], length: 4
Enter fullscreen mode Exit fullscreen mode

Immutable

list = ['a', 'b', 'c']
newList = ['x', ...list]
// newList: ['x', 'a', 'b', 'c']
Enter fullscreen mode Exit fullscreen mode

reverse

Mutable: reverse(): reversedList

list = ['a', 'b', 'c']
list.reverse()
// list: ['c', 'b', 'a']
Enter fullscreen mode Exit fullscreen mode

Immutable

list = ['a', 'b', 'c']
newList = [...list].reverse()
// newList: ['c', 'b', 'a']
Enter fullscreen mode Exit fullscreen mode

sort

Mutable: sort(): sorted

list = ['b', 'a', 'c']

list.sort() // ASC
// list: ['a', 'b', 'c']

list.sort((a, b) => b - a) // DESC
// list: ['c', 'b', 'a']
Enter fullscreen mode Exit fullscreen mode

Immutable

list = ['b', 'a', 'c']
asc  = [...list].sort()
desc = [...list].sort((a, b) => b - a)
// asc:  ['a', 'b', 'c']
// desc: ['c', 'b', 'a']
Enter fullscreen mode Exit fullscreen mode

splice / slice

Mutable: splice( startIdx, deleteCount = 1 ) :listFromStartToEnd

list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
spliced = list.splice(2, 3)
// [           'c', 'd', 'e'          ] // <= spliced
// ['a', 'b',                'f', 'g' ] // <= list
Enter fullscreen mode Exit fullscreen mode

Immutable: slice(startIdx, endIdx?)

list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
sliced = list.slice(2, 5)
remain = [...list.slice(0,2), ...list.slice(5, 7)]
// [           'c', 'd', 'e'          ] // <= sliced
// ['a', 'b',                'f', 'g' ] // <= remain
Enter fullscreen mode Exit fullscreen mode

Conclusion

Don't hate mutable JavaScript methods, use a spread operator well.


This content originally appeared on DEV Community and was authored by Nash


Print Share Comment Cite Upload Translate Updates
APA

Nash | Sciencx (2021-02-19T19:37:12+00:00) How to manipulate immutably and mutably JavaScript Array with only ES6+. Retrieved from https://www.scien.cx/2021/02/19/how-to-manipulate-immutably-and-mutably-javascript-array-with-only-es6/

MLA
" » How to manipulate immutably and mutably JavaScript Array with only ES6+." Nash | Sciencx - Friday February 19, 2021, https://www.scien.cx/2021/02/19/how-to-manipulate-immutably-and-mutably-javascript-array-with-only-es6/
HARVARD
Nash | Sciencx Friday February 19, 2021 » How to manipulate immutably and mutably JavaScript Array with only ES6+., viewed ,<https://www.scien.cx/2021/02/19/how-to-manipulate-immutably-and-mutably-javascript-array-with-only-es6/>
VANCOUVER
Nash | Sciencx - » How to manipulate immutably and mutably JavaScript Array with only ES6+. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/19/how-to-manipulate-immutably-and-mutably-javascript-array-with-only-es6/
CHICAGO
" » How to manipulate immutably and mutably JavaScript Array with only ES6+." Nash | Sciencx - Accessed . https://www.scien.cx/2021/02/19/how-to-manipulate-immutably-and-mutably-javascript-array-with-only-es6/
IEEE
" » How to manipulate immutably and mutably JavaScript Array with only ES6+." Nash | Sciencx [Online]. Available: https://www.scien.cx/2021/02/19/how-to-manipulate-immutably-and-mutably-javascript-array-with-only-es6/. [Accessed: ]
rf:citation
» How to manipulate immutably and mutably JavaScript Array with only ES6+ | Nash | Sciencx | https://www.scien.cx/2021/02/19/how-to-manipulate-immutably-and-mutably-javascript-array-with-only-es6/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.