Skip to content

Commit f7ce465

Browse files
Added Array methods
1 parent c84b94e commit f7ce465

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

_posts/2019-11-28-Commonly used JavaScript functions.markdown

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,32 @@ Use `indexOf()` if you need the position of the passed string in the string. Use
2525
- [substring()][String.prototype.substring()] - This method returns the part of the string between the start and end indexes.
2626
- [slice()][String.prototype.slice()] - This method is quite similar to `substring()` with some [differences][StackOverflow - slice() vs substring()].
2727

28+
## Array methods
29+
Following are some of the common methods from the [Array][Array] global object.
30+
31+
- [forEach()][Array.prototype.forEach()] - This method executes a provided function once for each array element.
32+
- [indexOf()][Array.prototype.indexOf()] - This method returns the first index at which a given element can be found in the array, or -1 if it is not present.
33+
- [includes()][Array.prototype.includes()] - This method returns _true_ or _false_ based on whether the passed value is contained in the array.
34+
35+
Just like with `String` methods, use `indexOf()` if you need the position of the value in the array. Use `includes()` if you are only concerned whether the value exists, it makes intent of the code more clear.
36+
37+
`indexOf()` and `includes()` use [strict equality][Strict equality] (the same method used by the === or triple-equals operator) for comparing the search element with the elements of the Array. So, when used on an array containing objects, object references are compared not the actual values.
38+
39+
Therefore, when dealing with array of objects, use `some()`, `find()`, or `findIndex()` methods.
40+
41+
- [find()][Array.prototype.find()] - This method returns the value of the first element in the array that satisfies the provided testing function. Otherwise, `undefined` is returned.
42+
- [findIndex()][Array.prototype.findIndex()] - This method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1.
43+
- [some()][Array.prototype.some()] - This method tests whether **at least one element** in the array passes the test implemented by the provided function. It returns _true_ or _false_ appropriately.
44+
- [every()][Array.prototype.every()] - This method tests whether **all elements** in the array passes the test implemented by the provided function. It returns _true_ or _false_ appropriately.
45+
46+
Following methods are used for filtering and transforming an array.
47+
48+
- [filter()][Array.prototype.filter()] - This method creates and returns a new array with all elements that pass the test implemented by the provided function.
49+
- [map()][Array.prototype.map()] - This method creates and returns a new array with the results of calling a provided function on every element in the calling array.
50+
- [reduce()][Array.prototype.reduce()] - This method executes a provided reducer function on each element of the array, resulting in a single output value. This final output value is returned.
51+
- [splice()][Array.prototype.splice()] - This method modifies the contents of the array by adding or removing new elements in-place.
52+
- [join()][Array.prototype.join()] - This method is the opposite of [String.prototype.split()][String.prototype.split()]. It creates and returns a new string by concatenating all array elements, separating them by a specified separator string.
53+
2854
[String]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
2955
{:target="_blank"}
3056
[String.prototype.indexOf()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
@@ -40,3 +66,32 @@ Use `indexOf()` if you need the position of the passed string in the string. Use
4066
[String.prototype.slice()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
4167
{:target="_blank"}
4268
[StackOverflow - slice() vs substring()]: https://stackoverflow.com/a/2243835/2924577
69+
70+
[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
71+
{:target="_blank"}
72+
[Array.prototype.forEach()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
73+
{:target="_blank"}
74+
[Array.prototype.indexOf()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
75+
{:target="_blank"}
76+
[Array.prototype.includes()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
77+
{:target="_blank"}
78+
[Strict equality]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity
79+
{:target="_blank"}
80+
[Array.prototype.find()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
81+
{:target="_blank"}
82+
[Array.prototype.findIndex()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
83+
{:target="_blank"}
84+
[Array.prototype.some()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
85+
{:target="_blank"}
86+
[Array.prototype.every()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
87+
{:target="_blank"}
88+
[Array.prototype.filter()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
89+
{:target="_blank"}
90+
[Array.prototype.map()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
91+
{:target="_blank"}
92+
[Array.prototype.reduce()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
93+
{:target="_blank"}
94+
[Array.prototype.splice()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
95+
{:target="_blank"}
96+
[Array.prototype.join()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
97+
{:target="_blank"}

0 commit comments

Comments
 (0)