Most useful Array methods in Javascript


Most Useful Javascript Array Methods

javascript arr methods

Array Sorting


String Array Sorting

Sort method is used to sort array elements in ascending order. 

var fruitsArray = ['Lemon','Mango','Orange','Apple'];
fruitsArray.sort();
console.log(fruitsArray);

Output: [ 'Apple', 'Lemon', 'Mango', 'Orange' ]

You can sort array in descending order by using reverse method. First sort array then reverse its element as

var fruitsArray = ['Lemon','Mango','Orange','Apple'];
fruitsArray.sort();
fruitsArray.reverse();
console.log(fruitsArray);

Output: [ 'Orange', 'Mango', 'Lemon', 'Apple' ]


Number Array Sorting

By default, sort method is used to sort string array only. To sort numeric array, we just have to pass a compare function to sort function as parameter. Below is ascending order sorting of numeric array.

var numbers = [6,8,2,5,9];
numbers.sort(function(x,y){
    return x-y;
})
console.log(numbers);

Output: [ 2, 5, 6, 8, 9 ]

To sort numeric array in descending order, just return y-x as

var numbers = [6,8,2,5,9];
numbers.sort(function(x,y){
    return y-x;
})
console.log(numbers);

Output: [ 9, 8, 6, 5, 2 ]


Array Find Largest & Smallest number

Highest number in numeric can be found by Math.max.apply method as

var numbers = [6,8,2,5,9];
var maxNumber = Math.max.apply(null,numbers);
console.log(maxNumber);

Output: 9

Similarly you can find smallest number by Math.min.apply method as

var numbers = [6,8,2,5,9];
var minNumber = Math.min.apply(null,numbers);
console.log(minNumber);

Output: 2


Javascript Map Array

Array map method get a callback function as parameter. On each element of array, this function is called.

Below three parameters are passed to this function. You can use 1 or 2 or all parameters.

  • Array element
  • Index of element
  • Array itself

In below example, map method of array multiply each element of array by 2 and show in console. We are omitting array parameter as not being used in callback function.

var numbers = [6,3,9,2];
numbers.map(multiplyByTwo);
function multiplyByTwo(elementindex){
 console.log('Element at index '+index+ ' is '+element);
}

Output: 

Element at index 0 is 6
Element at index 1 is 3
Element at index 2 is 9
Element at index 3 is 2


Javascript Filter Method

Filter method apply to an array. It creates a new array based on certain condition. Like map method, filter method also gets a callback function on every element of array that can use three parameters.

In below example, callback function return even numbers of array. Here we omit parameters index and array as not being used.

var numbers = [6,3,9,2,1,6,12];
numbers.filter(evenNumbers);
function evenNumbers(element){
    if(element%2 == 0){
        console.log(element);
    } 
}

Output:

6
2
6
12


Reduce Method Javascript

Reduce method calls a callback function on every element of array and reduce the array to a single value without changing the original array.

Callback function takes 4 parameters

  • Total (sum till previous element)
  • Element
  • Index of element
  • Array itself

Below example adds all elements of array and return it. Here callback function does not use index and array parameter so omit them.

var numbers = [3,9,4,1,6];
function getSum(totalelement){
    return total+element
}
var sum = numbers.reduce(getSum);
console.log(sum);

Output: 23


Array Every Method

Every method operates on every element of array and return true if all elements meet a certain condition. 

var numbers = [5,9,4,6];
function lessThanFour(element){
    return element < 4;
}
var result = numbers.every(lessThanFour);
console.log(result);

Output: false

Output is false as element 4 is not less then 4 so every method returns false.


Javascript Array Some

Some method return true if some elements of array meet a certain condition.

var numbers = [5,9,4,8];
function lessThanSix(element){
    return element < 6;
}
var result = numbers.some(lessThanSix);
console.log(result);

Output: true

Output is true as some elements meet the condition


Find Array Method

Javascript array find method returns the value of first element that meets the condition.

var numbers = [7,9,4,8];
function lessThanSix(element){
    return element < 6;
}
var result = numbers.find(lessThanSix);
console.log(result);

Output: 4


Array FindIndex

findIndex method returns index of first element that meets the certain condition. 

var numbers = [7,9,4,8];
function lessThanSix(element){
    return element < 6;
}
var result = numbers.findIndex(lessThanSix);
console.log(result);

Output: 2 (as element 4 is at index 2)


No comments:

Post a Comment