Arrays are one of the most used, and one of the most important data structures. All kinds of programming languages provide different ways to work with arrays. I have often seen students and early adopters of Javascript writing long functions, iterating over the array to solve some trivial problems. Javascript, with the kind of community support it has, provides a rich set of functions to work with arrays. In this post, we will read about a few Javascript Array Functions.

    1. array.every()

      Every() method lets you check if all the elements of an array satisfy given criteria. The criteria are passed to the method as a function that returns true or false. When called, the method runs the function once for each element of the array. If the function returns false for any element, the method returnsFalse, else it returns True

      const isPositive = (number) => {
      return number >= 0;
      }
      const numbers1 = [1,2,3,4,5];
      const numbers2 = [1,2,-3,4,5];
      console.log(numbers1.every(isPositive)); // True
      console.log(numbers2.every(isPositive)); // False 
    2. array.some()

      Some() method lets you check if any of the elements of an array satisfies the given criteria. The criteria are again passed to the method as a function that returns true or false.  When called the method runs the function once for each element of the array. If the function returns true for any element, the method returns True  and stops execution, else it returns False

      const isPositive = (number) => {
      return number >= 0;
      }
      const numbers1 = [-1,-2,-3,4,5];
      const numbers2 = [-1,-2,-3,-4,-5];
      console.log(numbers1.some(isPositive)); // True
      console.log(numbers2.some(isPositive)); // False 
    3. array.find()

      Just like Every() method, Find() also checks if an element in the array pass a certain criteria. The only twist is, it returns the first element that passes the criteria. It takes in a function that mimics the criteria to be tested. If the function returns true for any of the element of the array, the method returns that value. If no array element meets the criteria, the method returns undefined.

      const isNegative = (number) => {
      return number < 0;
      }
      
      const numbers1 = [1,2,3,4,5];
      const numbers2 = [1,2,-3,4,5];
      
      console.log(numbers1.find(isNegative)); // undefined
      console.log(numbers2.find(isNegative)); // -3
    4. array.includes()

      It is one of the Javascript Array Functions that tells if an array contains a specific element or not. If the element is present it returns true, else false.

      const numbers1 = [1,2,3,4,5];
      console.log(numbers1.includes(2)); // true
      console.log(numbers2.find(9)); // false
    5. array.reduce()

      Reduce method condenses the array into a single element. This always runs from left to right. As with all the methods till now, this too takes a callback function  as a parameter. Along with callback function ( which from now onwards will be referred to as cbFunction) reduce method also takes an optional parameter initialValue . This is the initialValue the aggregation that reduce method will return.

      Suppose you need the sum of all elements in an array + 1, in that case, you will pass initialValue as 1.

      Let us discuss a bit about thiscbFunction .
      cbFunction takes 4 parameters, 2 of which are optional.

      1. accumulator: It  accumulates the return of cbFunction . It is the aggregation of values returned by cbFunction in the previous invocation or    initialValue  if supplied.
      2. currentValue: Current value for which the cbFunction has been invoked.
      3. index (optional): Index of the current element for which the cbFunction is invoked.
      4. array(optional): Array for which reduce has been invoked

      const diff = (a,b) => {
      return a-b;
      }
      const numbers1 = [1,2,3];
      console.log(numbers1.reduce(diff)); // -4
      console.log(numbers1.reduce(sum,5)); // -1
      
    6. array.reduceRight():

      This method is exactly similar to reduce method, but it executes from right to left.

      const diff = (a,b) => {
      return a-b;
      }
      
      const numbers1 = [1,2,3];
      console.log(numbers1.reduceRight(diff)); // 0
      console.log(numbers1.reduceRight(sum,5)); // -1

Javascript has one of the largest communities among all the tools and programming languages. There are tons of features/methods which the language provides so that the developer can only focus on business logic. We have already covered some of the Javascript tricks in our articles here.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

Wordpress Social Share Plugin powered by Ultimatelysocial
error

Enjoy this blog? Please spread the word :)