Array Methods and Properties
Length
All methods return new array it does not change the original array.
length
property is used to find the total number of elements in an array.
let arr = ["laptop", "mobile", "tablet", "Computer"];
console.log(arr.length); //return 4
pop()
pop()
method is used to get element of an array from the last index.
let arr = ["laptop", "mobile", "tablet", "Computer"];
console.log(arr.pop()); //return Computer
push()
push()
method is used to add element of an array at the end of array. push is modify the array elements.
let arr = ["laptop", "mobile", "tablet", "Computer"];
arr.push(20);
console.log(arr);
shift()
shift()
method removed the first element from the array and return it. shift() method is modified the existing array.
let arr = ["laptop", "mobile", "tablet", "Computer"];
console.log(arr.shift()); //laptop
unshift()
unshift()
method is add a new array element at the beggining of the array and modify it. unshift method returns new array length.
let arr = ["laptop", "mobile", "tablet", "Computer"];
arr.unshift("pad"); //return new array
splice()
splice()
method insert the element in the array at given position and delete the array element according the given argument. splice method arguments are first argument is position for inserting element, second is how many element should be delete, and third is element.
let arr = ["laptop", "mobile", "tablet", "Computer"];
arr.splice(3, 0, "pad"); //modify the array
concate()
concate()
method add two or more arrays and return a new array.
let arr0 = ["laptop", "mobile", "tablet", "Computer"];
let arr1 = [20000, 30000, 340000, 42000];
let arr2 = arr0.concat(arr1); //add and return new array
slice()
slice()
method is create a new array from existing array and return it. It does not change existing array. It arguments, first is start index and second is end index.
let arr = ["laptop",20000, "mobile",42000, "tablet", "Computer"];
let arr1 = arr.slice(0,3); //return new array
sort() With Function Parameter
sort()
method sorts the array according to the function argument. function should return negative, zero, or positive number.
let arr = [87, 20, 76, 45, 42, 12, 93];
arr.sort(function(a, b){return a-b});
some()
some()
method takes a function as an argument and perform the operation on each element of array. if any element operation is true it return true.
et arr = [87, 20, 76, 45, 42, 12, 93];
let arr1 = arr.some(function (a) { return a > 50 }); // return true
every()
every()
method is perform operation of all array element according to given function. Example checking the all value less than 50.
let arr = [8, 20, 60, 45, 42, 12, 13];
console.log(arr.every(check));
function check(n) {
if(n < 50)
return true;
}
reduce()
reduce()
method is perfom the operation on all array elements. Add the all array element and return it.
let arr = [87, 20, 76, 45, 42, 12, 93];
console.log(arr.reduce(add)); // return addition
function add(pre_value, next_value) {
return pre_value + next_value
}
map()
map()
method is map the function with all array elements and return the new array. Here is the example devide all array element by 10.
let arr = [87, 20, 76, 45, 42, 12, 93];
console.log(arr.map(fun)); // return new array
function fun(n) {
return n/10;
}
filter()
filter()
method is use to filter the elements from an array. It return new array after the filter. Here is the example to filter the number that is greater than or equal 50.
let arr = [87, 20, 76, 45, 42, 12, 93];
console.log(arr.filter(fun)); // return 87, 76, 93
function fun(n) {
if (n >= 50)
return n;
}
foreach()
foreach()
method is used to access all array elements.
let arr = [87, 20, 76, 45, 42, 12, 93];
arr.forEach(show);
function show(n) {
console.log(n);
}
includes()
includes()
method checks item in an array. return true or false.
let arr = [8, 20, 60, 45, 42, 120, 13];
console.log(arr.includes(45)); // return true
Array.IsArray()
checking whether a object is an array or not. return true or false.
let arr = [8, 20, 60, 45, 42, 120, 13];
console.log(Array.isArray(arr));