In any programming language if we have a collection of elements or items, then we will define them as array. they can store data such as string, numbers, object as an collection of element and we can retrieve them whenever and however we want.
I will discuss about what can we do using array in JavaScript. so first of all let's create a array in JavaScript.
There are many ways to create array in JavaScript i.e. -
1. Basic Way
`const animals = ['π','π','π','π΅']`
2. Array Constructor
`const food = new Array('π','π','π','π','π')`
3. Empty Array
`const emptyArray = []`
Now, we know how to create an array in JavaScript and now let's dive into some methods of Array.
1. indexOf() - we can find an item's index
`const animals = ['πΆ','π±','π¦','π·']
animals.indexOf('π¦') //2 `
2. join() - create a string from array items and also can add words between them like 'and','or'.
`const animals = ['πΆ','π±','π¦','π·']
animals.join('loves') // 'πΆ loves π± loves π¦ loves π·' `
3. slice() - split an array at given index(es)
`const animals = ['πΆ','π±','π¦','π·']
animals.slice(1); //['π±','π¦','π·']
animals.slice(1,2); //[ 'π±' ]
console.log(animals) //[ 'πΆ', 'π±', 'π¦', 'π·' ] `
4. splice() - same as slice and you can also insert new items
`const animals = ['πΆ','π±','π¦','π·']
animals.splice(1,2, 'π') ) //[ 'π±', 'π¦' ]
console.log(animals) //[ 'πΆ', 'π', 'π·' ]`
_as you can see that slice and splice looks similar but slice don't overwrite or change original array but splice does. _
5. forEach() - loop over an array and access each item
`const animals = ['πΆ','π±','π¦','π·']
animals.forEach(pet => console.log(pet))`
6. Map() - loop over an array and access each item and return value without mutating original array
`const animals = ['πΆ','π±','π¦','π·']
animals.map(pet => console.log(`${pet} is cute`))`
As you can see in first glance both forEach and map method are same, but as you can see in map method we are mutating array instead we are creating a new array and using map we can return something.
7. flat()- It flatten an array to a single dimension.
`const animals = ['πΆ','π±','π¦', ['π','π'],'π·']`
as you can see we have array under an array , so how do we get a single dimension array.
`console.log(animals.flat()) //['πΆ','π±','π¦','π','π','π·']`
8. filter() - create a new array based on a filter.
`const animals = ['πΆ','π±','π¦','πΆ']
const dog = animals.filter(dog => dog === 'πΆ')
console.log(dog)`
9. reduce() - run a callback on each item and reduce the array to a single value.
`const numbers = [1,6,7,1,3,4]
const total = numbers.reduce((total,currentval) => total + currentval); //22 `
Ok, so what's happening here is we are taking every number one by one which is our currentval and adding it into our total.
10. findIndex() - finds the index of an item based on a condition
`const animals = ['πΆ','π±','π¦','π']
const lionIndex = animals.findIndex(lion => lion === 'π¦')
console.log(lionIndex) //2`
Conclusion
So they are the most used array methods which I use in my code and I hope you will find them useful and we are all learning, I missed a lot of methods here but doing research is a part of the process π§βπ»π§βπ»
Top comments (0)