Professional Documents
Culture Documents
Untitled Document
Untitled Document
5. concat() eturns the first element in the array that satisfies a
R var array1 = [1, 2];
provided testing function.
Syntax: array.find(function(currentValue, index, var array2 = [3, 4];
arr),thisValue);
8. join() eturns the first element in the array that satisfies a
R v ar array = [ 1, 2, 3 ];
provided testing function. var joinedString = array.join( ‘-‘ );
Syntax: array.find(function(currentValue, index, Output: joinedString = “1-2-3”
arr),thisValue);
9. indexOf() eturns the first index at which a given element is found in
R v ar array = [ 1, 2,3, 4, 5 ];
the array. var index = array.indexOf( 3 );
Syntax: array.indexOf(item, start_position_for_search) Output:index = 2 // Index of 3
start_position_for_search = default 0;
v ar index = array.indexOf( 3, 3 );
eturns: index of search element.
R Output:index = -1 // Index of 3 searched
Returns -1 if the value is not found. from index 3 hence not found.
– Does not change the original array.
10. eturns the last index at which a given element is found in
R v ar array = [ 1, 2, 3, 4, 5,3];
the array. (indexes –> 0, 1, 2, 3, 4, 5)
Syntax: array.lastIndexOf(item, start_position_for_search) var lastIndex = array.lastIndexOf( 3 );
– start_position_for_search = Default is the last element. Output: lastIndex = 5 // Last index of 3
12. forEach() xecutes a provided function once for each array element.
E v ar array = [ 1, 2, 3 ];
Syntax: array.forEach(function(currentValue, index, arr), array.forEach(element =>
thisValue); console.log(element));
Output: 1, 2, 3
eturns:undefined.
R
– Does not change the original array.
13. map() reates a new array with the results of calling a provided
C v ar array = [ 1, 2, 3 ];
function on every element in the array. var newArray = array.map(element =>
Syntax: array.map(function(currentValue, index, arr), element * 2);
thisValue); Output: newArray = [ 2, 4, 6 ];
14. filter() reates a new array with all elements that pass a test
C v ar array = [ 1,2, 3,4, 5 ];
implemented by the provided function. var filteredArray = array.filter(element =>
Syntax: array.filter(function(currentValue, index, arr), element % 2 === 0);
thisValue); Output: filteredArray = [2, 4];
16. imilar to reduce(), but processes the array from right to
S v ar array = [ 1, 2, 3, 4, 5 ];
left. var result = array.reduceRight((acc, curr)
=> acc – curr);
eturns: The accumulated result.
R Output: result = -13
– Does not change the original array.
17. find() eturns the index of the first element in the array that
R v ar array = [ 1, 2, 3,4, 5 ];
satisfies a provided testing function. var foundElement = array.find(element =>
Syntax: array.findIndex(function(currentValue, index, arr), element > 3);
thisValue) Output: foundElement = 4
eturns: The value of the first element that passes the test.
R
Otherwise it returns undefined.
– Does not change the original array.
20. every() hecks if all elements in the array satisfy a provided testing
C v ar array = [ 1, 2, 3, 4, 5 ];
function. var allPositive = array.every(element =>
Syntax: array.every(function(currentValue, index, arr), element > 0);
thisValue); Output: allPositive = true
23. isArray() C
hecks if a value is an array. ar array = [ 1, 2, 3 ];
v
Syntax: Array.isArray(obj); var isArrayTrue = Array.isArray(array);