15 JavaScript Array Methods you should know

Arrays are wonderful and a very particular type in JavaScript. There are many useful built-in properties and methods that will help you resolve any task which involves arrays. Today we are going to discuss 15 array methods every developer should know.

The following 15 array methods are the most commonly used ones!

1.index of() find an item’s index

const myPets = ['Dog', 'Cat', 'Hamster'];
my Pets.index0f ('Cat')→ // 1

2.join() – create a string from array items

const myPets = ['Dog', 'Cat', 'Hamster'];
myPets.join(' and ');  //→ 'Dog and Cat and Hamster'

3.slice() – split an array at given index(es)

const myPets = ['Dog', 'Cat ', 'Hamster'];
myPets.slice(1) ; →  // ['Cat', "Hamster']
myPets.slice(1, 2); →  //['Cat']

4.splice() – split an array and/or insert new items

const myPets = ['Dog', 'Cat, 'Hamster'] ;
myPets.splice (1, 2, 'Lizard') →   // [ 'Dog', Lizard']

5.concat() – concatenate one or more arrays

const myPets = ['Dog', 'Cat', Hamster'];
const myFlyingPets = ['Bird'];
const myWaterPets = ['Fish '];

const allPets = my Pets.concat (myFlyingPets, myWaterPets);
// allPets → ['Dog ', 'Cat', 'Hamster', 'Bird', 'Fish']


6.forEach()- loop over an array and access each item

const myPets = ['Dog', 'Cat ', 'Hamster'];
my Pets. forEach(pet ⇒ console. log(pet)) ;

7.filter() – create a new array based on a filter

const myPets = ['Dog', 'Cat', 'Hamster'];
const threeLetterPets = myPets. filter (pet ⇒ pet. length ≡ 3)
// threeLetterPets → ['Dog', 'Cat']

8.map() – loop over an array and run some operation on each item without mutating the original array

const myPets = ['Dog', 'Cat', 'Hamster'] ;
const lovedPets = myPets.map(pet ⇒ '${pet}❤️')
// lovedPets  → ['Dog❤️' , Cat❤️' ,'Hamster❤️']

9.flat()- flatten an array to a single dimension

const values = [1, 2, [7], 3, [1, 2], 4]
console. log (values.flat())
// [1, 2, 7, 3, 1, 2, 4]

10.reduce() – run a callback on each item and reduce the array to a single value

const values = [1, 6, 7, 1, 3, 4];
const total = values.reduce(total, currentVal) ⇒ total + currentNum);
//total →  22

11.findlndex() – finds the index of an item based on a condition

const people = [{ name: 'David' }, { name: 'Peter'}, {name: 'Alex }]
const peterIndex = people. findIndex (person ⇒ person.name ≡'Peter');
// peterIndex → 1

12.every()- check if every item meets a condition

const values = [1, 6, 7, 1, 3, 4];
console. Log(values.every(val ⇒ val < 8))
//true

13.find()- find first value that meets a condition

const values = [4, 1, 7, 2, 5, 7, 9, 25];
const firstValueOverSeven = values. find (val ⇒ val > 7);
// firstValueO verSeven → 9

14.some()- check if some values meet a condition

const values = [1, 6, 7, 1, 3, 4];
console.log (values. some (val ⇒ val > 7))
// false

15.sort() – sorts an array

const values = [4, 1, 7, 2, 5];
const names = ['David', 'Alexander', 'Peter'];

values.sort((a, b) ⇒ a - b); // [1, 2, 4, 5, 7]
names.sort (); // ['Alexander', 'David', 'Peter']

Leave a Reply

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