Skip to content

Instantly share code, notes, and snippets.

// fetch api
{
posts: [
{
id: 88,
content: 'North Korea News',
author: {
id: 41,
name: 'Kim Jong-un',
},
@nattatorn-dev
nattatorn-dev / Pure javascript immutable arrays
Created April 17, 2017 03:52
Pure javascript immutable arrays
/** all pure func avoid push, pop, shift, unshift, sort, reverse, splice and delete */
/**
* @param {[array]} array | ['USD', 'JPY', 'GBP'] |
* @param {[number]} index | 0 |
* @return {[array]} array | ['JPY', 'GBP'] | delete element of array by index and return new array
*/
const deleteByIndex = ( array, index ) =>
array.slice( 0, index ).concat( array.slice( index + 1 ) )
Head & Tail
Destructuring syntax allows us to get the head and tail of a list without utility functions.
_.head([1, 2, 3]);
// 1
_.tail([1, 2, 3]);
// [2, 3]
// becomes