JavaScript Array Methods – In this tutorial we are going to show you the daily useful and important JavaScript Array Methods with examples. Javascript array methods or functions, to help write clean code with less effort.
In this tutorial, we will demonstrate daily useful 18 array methods of javascript with examples.
The basic daily useful array methods in javascript are the following:
The “pop()” method is used when you want to remove the last element of an array.
The method returns the removed items value.
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
lang.pop();
Result of the above example is:
Output
// New array: Python,Vuejs,Nodejs
The “push()” method is used to add a new element to the end of an array.
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
lang.push("angular");
Result of the above example is:
Output
// New array: Python,Vuejs,Nodejs,javascript,angular
The “toString()” method is used to convert array to string.
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
lang.toString();
Result of the above example is:
Python,Vuejs,Nodejs,javascript
Javascript – Remove Duplicates from Array
The join() used to join the elements of an array into a string.
The “join()” method puts all the elements of the array into a string list. This method difference from “toString()” method.
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
lang.join("-");
var x = document.getElementById("demo");
x.innerHTML = lang.join("-");
Result of the above example is:
// output
Python-Vuejs-Nodejs-javascript
The “splice()” method can add and remove items form an array.
array.splice(index, howMany, [element1][, …, elementN]);
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
lang.splice(2, 0, "Mongodb", "Rust");
Result of the above example is:
// Output
new array: Python,Vuejs,Mongodb,Rust,Nodejs,javascript
This method either alphabetic or numeric sorts an array.
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
lang.sort();
Result of the above example is:
// Outuput
array: Nodejs,Python,Vuejs,javascript
This method removes the first element of an array. like the “pop()” method, removes an element from an array.
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
lang.shift();
Result of the above example is:
// Output
new array: Vuejs,Nodejs,javascript
The unshift() method adds a new element to the beginning of an array.
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
lang.unshift("Rust");
Result of the above example is:
//Output
New array: Rust,Python,Vuejs,Nodejs,javascript
The method is used for reverses the order of the elements in an array.
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
lang.reverse();
Result of the above example is:
// Outuput
array: javascript,Nodejs,Vuejs,Python
The “concat()” method joins two or more arrays and makes a new one.
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
var newlang = ["Angular", "Rust"];
var join = lang.concat(newlang);
Result of the above example is:
// Output
New array: Python,Vuejs,Nodejs,javascript,Angular,Rust
The method is used to selected elements in an array and makes a new one. It can take one or two arguments.
var lang = ["Python", "Vuejs", "Nodejs", "javascript"];
var cars = lang.slice(1, 4);
Result of the above example is:
//Output
New Array : Vuejs,Nodejs,javascript
This filter method is used to filter the array, according our conditions.
var number = [10, 5, 16, 4, 6, 7, 12, 18, 20];
function isCheck(value) {
return value > 9;
}
var filter = number.filter(isCheck);
document.getElementById("demo").innerHTML = filter;
Result of the above example is:
Output
//new array: 10,16,12,18,20
In this example the function filter() creates a new array. Those elements that satisfy the condition checked by isCheck() function.
This find method is used find the first element of an array.
var number = [10, 5, 16, 4, 6, 7, 12, 18, 20];
function isCheck(value) {
return value >= 9;
}
var find = number.find(isCheck);
document.getElementById("demo").innerHTML = find;
Result of the above example is:
Output
// 10
The JavaScript Array Method – forEach () imposes a specific function for each item in a particular array individually:
var num = [10, 5, 16, 4, 6, 7, 12, 18, 20];
num.forEach(function(item) {
document.writeln(item);
});
Result of the above example is:
Output
// return
10 5 16 4 6 7 12 18 20
The map () method in JavaScript creates an array by calling a specific function on each element in the original array.
var num = [4, 9, 16, 25];
var x = num.map(Math.sqrt);
document.write(x);
Result of the above example is:
Output
// return
2,3,4,5
The javascript array reduce() method reduces the array to a single value.
var numArr = [
{ name: 'a', num: 50},
{ name: 'b', num: 50},
{ name: 'c', num: 75},
{ name: 'd', num: 35},
{ name: 'e', num: 25 },
{ name: 'f', num: 40 },
];
var sum = numArr.reduce(function (total, currentValue) {
return total + currentValue.num;
}, 0);
document.write( "javascript- Sum of the array value is :- " + sum );
Result of the above example is:
Output
// return
javascript- Sum of the array value is :- 275
The javascript array some() method checks whether at least one element of the array matches the given predicate. if none of the array elements match the predicate, then it will return false
var nums = [10, 12, 12, 20, 25];
function checkNumber(num) {
return num >= 25;
}
document.write(nums.some(checkNumber));
Result of the above example is:
Output
// return
True
The javascript array every() method checks whether all elements of the array match the predicate:
var nums = [10, 12, 12, 20, 25];
function checkNumber(num) {
return num >= 3;
}
document.write(nums.every(checkNumber));
Result of the above example is:
// return
true
#javascript #programming