Ajay Mandaviya


Must know JavaScript Array  Methods

Must know JavaScript Array Methods

Ajay Mandaviya's photo
Ajay Mandaviya
ยทMay 26, 2022ยท

3 min read

Table of contents

  • Push
  • Pop
  • Map
  • Filter
  • Find
  • Sort
  • Some

Array is data structure that contain list of element, That store multiple values inside a Single variable There are lots of JavaScript array methods . We can apply those methods on our array . Every array method has unique functionality that perform some manipulation

Let's Say we Have an array

const user = [
  {name: "jon", age: 10},
  {name: "don", age: 18},
  {name: "kon", age: 49},
  {name: "mac", age: 30},
  {name: "dan", age: 20},
  {name: "dan", age: 50}


Push Method allowed you to add or push item into array . let's say if we want to add new user into array then we can use a push method for add into array and it will return a new array.

user.push({name : "new" , age : 25})

if we now we check user array length it's increase now it's become 7 push method push item into last .



Pop method remove the last element of an array .


Screenshot from 2022-05-27 00-31-59.png


Map Method allowed you to iterate over an array or we can The map method lets you loop on each element inside the array, and manipulate them as you wish.

Let's under stand with example, we have a two arrays cart and user

const productName = cart.map((element, index)=>{
      return element.name
// ["shirt", "laptop", "phone", "mac", "Headphones"]

const userNames = user.map((element, index)=>{
      return element.name
// ["jon", "don", "kon", "mac", "dan", "dan"]


In JavaScript, the filter() method allows us to filter through an array - iterating over the existing values, and returning only the ones that fit certain criteria, into a new array.

Let's say we need only user , which have age >40

it return only two user

let userage = user.filter((ele)=> {
  return ele.age > 40
} )



The find method creates a new object based on the condition we set. This method looks like the filter method, but they are not the same. By setting a condition,filter returns an array of all the matched elements, while find returns only the first matched element.

let userage = user.find((ele)=> {
  return ele.name === "jon"
} )
 // {name: "jon", age: 10}


Sort in JavaScript help to sort the array data .

1 Sorting user to Based on their age Lowest-Highest age.

        let useragesort = user.sort((a, b)=> {
           return a.age - b.age
          } )

sort a-b.png

2.Sorting Highest-Lowest user.

        let useragesort = user.sort((a, b)=> {
           return b.age - a.age
          } )

sort b-a.png


Some lterate a every element in array and check weather an array at least contain one a certain values or not . return true or false.

let checkuser = user.some((ele)=> {
  return ele.name === "don"
} )
// output: true

So that's it for now.

Thanks for reading till now ๐Ÿ™

You can connect ๐Ÿ‘‹ with me on LinkedIn, Twitter GitHub

Share this