Commonly used functions on Array
Creates an array by calling a specific function on each element present in the parent array.
array.map(function(currentValue)
Parameter | Description |
---|---|
function(currentValue) | Required function to be run for each array element |
An Array containing the results of calling the provided function for each element in the original array.
Creates an array filled with all array elements that pass a test (provided as a function).
array.filter(function(currentValue)
Parameter | Description |
---|---|
function(currentValue) | Required function to be run for each array element |
Array containing all elements that pass the test else an empty array.
let filtered = [112, 52, 0, -1, 944].filter(function (value) {
return value > 0;
});
Output:
[112,52,944]
reduces the array to a single value and executes provided function for each value of the array (from left-to-right).
array.reduce( function(total, currentValue, currentIndex, arr), initialValue )
Parameter | Description |
---|---|
function(currentValue) | Required function to be run for each array element |
returns accumulated result fron the last call of the callback function.
const array1 = [1, 2, 3, 4];
const reducer = function (accumulator, currentValue) {
return accumulator + currentValue;
};
console.log(array1.reduce(reducer));
console.log(array1.reduce(reducer, 5));
Output:
10 15
Returns the selected elements in an array, as a new array object and selects the elements starting at the given start argument, and ends at the given end argument(excluding end argument).
array.slice(start, end);
Parameter | Description |
---|---|
start | Optional. specifies starting index of the selection |
end | Optional. specifies ending index of the selection |
New array containing the selected elements.
let arr = [1, 2, 3, 4, 5, 6];
let new_arr = arr.slice(3);
Output:
[1,2,3,4,5,6]
[4,5,6]
Adds/removes items to/from an array, and returns the removed item(s).
array.splice(index, howmany, item1, ....., itemX)
Parameter | Description |
---|---|
index | Required. An integer that specifies at what position to add/remove items |
howmany | Optional. The number of items to be removed. |
item1, ..., itemX | Optional. The new item(s) to be added to the array |
Array without the removed items.
const languages = ['C++', 'Java', 'Html', 'Python', 'C'];
// Add 'Julia' and 'Php' after removing 'Html'.
const removed = languages.splice(2, 1, 'Julia', 'Php');
Output:
C++,Java,Html,Python,C
C++,Java,Julia,Php,Python,C
Note
splice | slice |
---|---|
Returns the removed item(s) in an array | Returns the selected element(s) in an array, as a new array object |
Changes the original array | Doesn’t change the original array |
Can take n number of arguments(1 required) | Can take 2 arguments(1 required) |
Used to add a single item to an array(or to add two or more arrays).
array1.concat(item);
or
array1.concat(array2, array3, ..., arrayX)
Parameter | Description |
---|---|
array2, array3, ..., arrayX | Required. The arrays to be joined. |
Joined array
const num1 = [1, 2, 3],
num2 = [4, 5, 6],
num3 = 7;
console.log(num1.concat(num2, num3));
Output:
[1,2,3,4,5,6,7]
Note
concat | .push |
---|---|
Adds elements to the end of an array | Adds element or merges arrays |
Returns the new length of the array | Returns a new array |
Returns the array as a string. The elementsare separated by a specified separator. The default separator is comma (,).
array.join(separator);
Parameter | Description |
---|---|
separator | Optional. |
String, representing the array values, separated by the specified separator.
const languages = ['C++', 'Java', 'Html', 'Python', 'C'];
languages.join();
languages.join('.');
languages.join('-');
Output:
C++,Java,Html,Python,C
C++.Java.Html.Python.C
C++-Java-Html-Python-C
Adds new items to the end of an array, and returns the new length.
array.push(item1, item2, ..., itemX)
Parameter | Description |
---|---|
item1, item2, ..., itemX | Required. The item(s) to add to the array |
New length of the array.
let arr = [1, 2, 3, 4, 5];
console.log(arr.push(6, 7, 8));
Output:
8
[1,2,3,4,5,6,7,8]
Returns the value of the first element in an array that pass a test (provided as a function).
array.find(function(currentValue)
Parameter | Description |
---|---|
function(currentValue) | Required. A function to be run for each element in the array. |
The array element value if any of the elements in the array pass the test, otherwise it returns 'undefined'.
let array = [1, 3, 5, 7, 9];
const found = array.find(function (element) {
return element > 4;
});
// Printing desired value.
Output:
5
returns the position of the first occurrence of a specified value in a string.
string.indexOf(searchvalue, start);
Parameter | Description |
---|---|
searchvalue | Required. The string to search for |
start | Optional. Default 0. At which position to start the search. |
Number, representing the position where the specified searchvalue occurs for the first time, or -1 if it never occurs.
console.log('Departed Train'.indexOf('Train'));
Output:
9
Extracts the characters in a string between "start" and "end", not including "end" itself.
string.substring(start, end);
Parameter | Description |
---|---|
start | Required. The position where to start the extraction. First character is at index 0 |
end | Optional. The position (up to, but not including) where to end the extraction. If omitted, it extracts the rest of the string |
New string containing the extracted characters.
// Taking a string as variable
const string = 'JavaScriptCheatsheet';
a = string.substring(0, 4);
b = string.substring(1, 6);
c = string.substring(5);
d = string.substring(0);
// Printing new string which are
// the part of the given string
Output:
Java avaSc criptCheatsheet JavaScriptCheatsheet
Note
array.slice() | string.substring() |
---|---|
Displays selected array elements | Displays selected part of string |
Returns new array | Returns new string |
Used to split a string into an array of substrings, and returns the new array.
string.split(separator, limit);
Parameter | Description |
---|---|
separator | Optional. Specifies the character, or the regular expression, to use for splitting the string. If omitted, the entire string will be returned (an array with only one item) |
limit | Optional. An integer that specifies the number of splits, items after the split limit will not be included in the array |
Array containing splitted values.
let str = 'It iS a great Day.';
let array = str.split(' ');
Output:
["It","iS","a","great","Day."]
Converts a string to lowercase letters.
string.toLowerCase()
A string, representing the value of a string converted to lowercase.
const str = 'It iS a Great Day.';
const string = str.toLowerCase();
Output:
it is a great day.
Removes whitespace from both sides of a string.
string.trim();
String, representing the string with removed whitespace from both ends
const str = ' "JavaScript" ';
const st = str.trim();
Output:
"Javascript"
Returns the character at the specified index in a string.
string.charAt(index);
String, representing the character at the specified index, or an empty string if the index number is not found
const str = 'JavaScript is object oriented language';
console.log(str.charAt(9));
Output:
t
for (var in object)
{
//code block to be executed
}
const array1 = ['a', 'b', 'c'];
for (const element in array1) {
console.log(element);
}
Output:
"a"
"b"
"c"
Loops through the values of an object.
for (variable of object) {
// code block to be executed
}
const languages = {
first: 'C',
second: 'Java',
third: 'Python',
fourth: 'PHP',
fifth: 'JavaScript'
};
// iterate through every property of the
// object languages and print all of them
// using for..in loops
for (itr in languages) {
console.log(languages[itr]);
}
Output:
C Java Python PHP JavaScript
Traditional index-based loop as found in many languages.
for (statement 1; statement 2; statement 3)
{
// code block to be executed
}
const i;
for (i = 0; i < 10; i++)
{
console.log("Hello World!\n");
}
This will print Hello World on the screen 10 times.
Calls a function once for each element in an array, in order.
array.forEach(function(currentValue)
Parameter | Description |
---|---|
function(currentValue) | Required. A function to be run for each element in the array. |
const items = [1, 29, 47];
const copy = [];
items.forEach(function (item) {
copy.push(item * item);
});
Output : It squares each number in the array items .
1,841,2209