Loading

10 most useful JavaScript array methods

10 most useful JavaScript array methods

In this post, I am going to share with you 10 most useful javascript array methods which will be very helpful in development.

  1. forEach(): You can loop your array using this method

Example:

cost numbers = [1,2,3,4,5];
numbers.forEach(item => {
    console.log(item); // output: 1 2 3 4 5 6
  });
  1. Spread operator: fast way to display the array elements without looping the array.

Example

const hobbies = ['Singing','Dancing','Playing','Reading','Cooking'];

console.log(...hobbies);

//output: Singing Dancing Playing Reading Cooking

You May Like: JavaScript null vs undefined vs blank vs value

  1. Includes(): This method is used to check if an element exists in an array. If exists return true else False;

Example

cost appliances = ['Fan','Mixer','AC','Washing Machine','TV'];

const hasMicroware = appliances.Includes('Microware');
//output false;
  1. filter(): it creates the new array of an element which satisfy the conditions.

Example

const salaries = [5000,6000,7000,8000,2000,4000,10000];

salaries.filter(function(salary){
    return salary < 5000;
});

//output [2000,4000]
  1. map(): The map method is used to modify the item & return the new array

Example

const salaries = [5000,6000,7000,8000,2000,4000,10000];

employeeSalaries = salaries.map(function(salary){
    return salary + 200;
});

//output [5100,6100,7100,8100,2100,4100,10100]
  1. reduce(): it is used to convert an array into an integer, an object, an array as required.

Example

const fruits = [
    { name: 'apple', price: 200 },
    { name: 'mango', price: 100 },
    { name: 'banana', price: 40 }
]

const fruitPrice = fruits.reduce((a, v) => {
    let name = fruits.name;
    a[v.name] = v.price;
    return a;
}, {});

console.log(fruitPrice);

//Result will be "{ apple: 200, mango: 100, banana: 40 }"
  1. every(): the Every method is checked every element with the provided condition and return true if all item satisfied the condition else return false.

Example

const salaries = [5000,6000,7000,8000,2000,4000,10000];

salaries.map(function(salary){
    return salary > 1000;
});

//output true;
  1. encodeURI(): It is used to encode the URI

Example

var uri = "https://www.google.com/search? javascript array method";
var res = encodeURI(uri);
console.log(res);
//output  https://www.google.com/search?%20javascript%20array%20method
  1. decodeURI: The decodeURI() method is used to decode a URI.

Example

var uri = "https://www.google.com/search?%20javascript%20array%20method" 
var res = encodeURI(uri);
console.log(res);
//output https://www.google.com/search? javascript array method;
  1. Object.keys(): This method returns the object key as an array.

Example

const profiles =  {
     name: "Abhay",
     email: "abhay@condinghub.net",
     'city': 'Kolkata'
 };

const newArray = Object.keys(profiles);
console.log(newArray);

//output ['name','email','city']
  1. Object.values() : This method return the object value as array

Example

const profiles =  {
     name: "Abhay",
     email: "abhay@condinghub.net",
     'city': 'Kolkata'
 };

const newArray = Object.values(profiles);
console.log(newArray);
// output ["Abhay", "abhay@condinghub.net", "Kolkata"]

Related Articles