Skip to content

Instantly share code, notes, and snippets.

@enqtran
Last active December 9, 2019 01:19
Show Gist options
  • Save enqtran/445e3536882c79be32e618d5794c0f95 to your computer and use it in GitHub Desktop.
Save enqtran/445e3536882c79be32e618d5794c0f95 to your computer and use it in GitHub Desktop.
1. Iterate
1.1 for..of cycle - You can stop iterating at any time using a break statement.
1.2 for cycle - You can stop iterating at any time using a break statement.
1.3 array.forEach() method - You cannot break array.forEach() iterating.
2. Map
2.1 array.map() method - array.map() creates a new mapped array, without mutating the original one.
2.2 Array.from() function - Array.from() creates a new mapped array, without mutating the original one. Array.from() fits better to map from an array-like object.
3. Reduce
3.1 array.reduce() method - The first array item becomes the initial value if you skip the initialValue argument.
4. Concat
4.1 array.concat() method - array.concat() creates a new array, without mutating the original one. array.concat(array1[, array2, ...]) accepts multiple arrays to concat.
4.2 Spread operator - [...arr1, ...arr2, ...arrN]: you can concat as many arrays as you need using spread operator.
5. Slice
5.1 array.slice() method - array.slice() creates a new array, without mutating the original one.
6. Clone
6.1 Spread operator - [...array] creates a shallow copy.
6.2 array.concat() method - [].concat(array) creates a shallow copy.
6.3 array.slice() method - colors.slice() creates a shallow copy.
7. Search
7.1 array.includes() method - returns true or false
7.2 array.find() method - array.find() returns undefined if no item has satisfied the predicate.
7.3 array.indexOf() method - array.indexOf(itemToSearch) returns -1 if the item hasn’t been found. array.findIndex(predicate) is an alternative to find the index using a predicate function.
8. Query
8.1 array.every() method - array.every(predicate) method returns true if every item passes predicate check.
8.2 array.some() method - array.some(predicate) method returns true if at least one item passes predicate check.
8.3 array.select(i => (i < 3)); // => [1, 2];
8.4 array.reject(i => (i < 3)); // => [3, 4, 5];
9. Filter
9.1 array.filter() - array.filter(predicate) method returns a new array with items that have passed predicate check. array.filter() creates a new array, without mutating the original one.
10. Insert
10.1 array.push() method - array.push() mutates the array in place. array.push(item1, item2, ..., itemN) can push multiple items.
10.2 array.unshift() method - array.unshift() mutates the array in place. array.unshift(item1, item2, ..., itemN) can insert multiple items.
10.3 Spread operator
11. Remove
11.1 array.pop() method - array.pop() method removes the last item from an array, then returns that item. array.pop() mutates the array in place.
11.2 array.shift() method - array.shift() mutates the array in place. array.shift() has O(n) complexity.
11.3 array.splice() method - array.splice() mutates the array in place.
11.4 Spread operator
12. Empty
12.1 array.length property
12.2 array.splice() method
13. Fill
13.1 array.fill() method - array.fill() mutates the array in place.
13.2 Array.from() function - Array.from() can be useful to initialize an array of certain length with object
14. Flatten
14.1 array.flat() method - array.flat() creates a new array, without mutating the original one.
15. Sort
15.1 array.sort() method - array.sort() mutates the array in place.
-1 then item1 will follow by item2 in the sorted array
1 then item2 will follow by item1 in the sorted array
0 then the position of items doesn’t change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment