Working With Arrays In JavaScript

Introduction

 
JavaScript Array object is a global object that is used in the construction of arrays; which are high-level, list-like objects. Some of the important methods used with arrays are discussed in this article.
 
The concat() method is used to merge two or more arrays. This method does not change the existing arrays but instead, returns a new array.
  1. <script>    
  2.     let developer = ['arvid''soniya''priya''rahul'];    
  3.     let testor = ['umesh''kalpana''suresh''zoya'];    
  4.     let employee = developer.concat(testor);    
  5.     console.log(employee)    
  6. </script>   
The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
  1. <script>    
  2.     let employee = ['arvid''soniya''priya''rahul''umesh''kalpana''suresh''zoya'];    
  3.     let iterator = employee.entries();    
  4.     for (let e of iterator) {    
  5.         console.log(e)    
  6.     }    
  7. </script> 
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
  1. <script>    
  2.     let employee = ['arvid''soniya''priya''rahul''umesh''kalpana''suresh''zoya'];    
  3.     console.log(employee.filter(e => e.length < 6));    
  4.     console.log(employee.filter(e => e.length > 6))    
  5.     console.log(employee.filter(e => e.length == 6))    
  6. </script>   
The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.
  1. <script>    
  2.     let numbers = [10, 12, 15, 18, 9, 5, 22, 13, 30];    
  3.     let found = numbers.find(function (element) {    
  4.         return element > 10;    
  5.     });    
  6.     console.log(found);    
  7. </script>   
The fill() method fills (modifies) all the elements of an array from a start index (default zero) to an end index (default array length) with a static value. It returns the modified array.
  1. <script>    
  2.     let numbers = [10, 15, 12, 3, 4, 7, 2];    
  3.     console.log(numbers.fill(0, 1, 3))    
  4.     console.log(numbers.fill(5, 1))    
  5.     console.log(numbers.fill(6))    
  6. </script>  
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating no element passed the test.
  1. <script>    
  2.     let emplyees = ['Mariya''priya''sonam''juliya''priyanka'];    
  3.     console.log(emplyees.indexOf('priya'))    
  4. </script>   
The sort() method sorts the elements of an array in place and returns the array. 
  1. <script>    
  2.     let employee = ['arvid''soniya''priya''rahul''umesh''kalpana''suresh''zoya'];    
  3.     const sortemployee = employee.sort();    
  4.     console.log(sortemployee)    
  5. </script>   
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
  1. <script>    
  2.   let employee = ['arvid''soniya''priya''rahul''umesh''kalpana''suresh''zoya'];    
  3.   const reverseemployee = employee.reverse();    
  4.   console.log(reverseemployee)    
  5. </script>  
The toString() method returns a string representing the specified array and its elements.
  1. <script>    
  2.     let employee = ['arvid''soniya''priya''rahul''umesh''kalpana''suresh''zoya'];    
  3.     console.log(employee.toString())    
  4. </script>  
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
  1. <script>    
  2.     let employee = ['arvid''soniya''priya''rahul''umesh''kalpana''suresh''zoya'];    
  3.     console.log(employee.shift())    
  4. </script> 
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
  1. <script>    
  2.     let employee = ['arvid''soniya''priya''rahul''umesh''kalpana''suresh''zoya'];    
  3.     let unsiftemployee = employee.unshift('afra''farhan');    
  4.     console.log(employee)    
  5. </script>  
The push() method adds one or more elements to the end of an array and returns the new length of the array.
  1. <script>    
  2.     let employee = ['arvid''soniya''priya''rahul''umesh''kalpana''suresh''zoya'];    
  3.     let newemployee = employee.push('Buvi''Chethan');    
  4.     console.log(employee)    
  5. </script>   
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
  1. <script>    
  2.     let employee = ['arvid''soniya''priya''rahul''umesh''kalpana''suresh''zoya'];    
  3.     let deleteemployee = employee.pop('zoya');    
  4.     console.log(employee)    
  5. </script> 
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
  1. <script>    
  2.     let table = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];    
  3.     console.log(table.map(x => x * 2));    
  4. </script>