Useful methods for Array manipulation

Shuvasish Talukder Shuvo
3 min readMay 4, 2021
return value of array methods

Probably array is the most widely used data structure in javaScript. Basically array is an object. Array has some built in methods.
Let’s talk about them.

map: map is a higher order function that takes a function as an input. This function loop through all array element and can do
some operations with each element. this returns a new array with new elements.
Return: returns a new array.
Time complexity: As it loop through all the elements of the array, So it’s time complexity is BIG O(n)

filter: Filter is another array method that takes an callback function and do some kind of condition. Which ever elements pass
the condition, those are saved is an array and returns this new array;
Return: a new array;
Time complexity: BIG O(n)

reduce: Reduce is vary powerful array method. That also take an function as input. This function can take 4 parameters.
first parameter is know as accumulator, accumulator is a variable that will hold the result of operation that will be done in
this function. second parameter is current element, third one is current element’s index and fourth one provides whole
array. this method returns a single value that is saved in accumulator;
Return: a single value ( could be any type of value)
Time complexity: BIG O(n)

find: Find is another array method. This will loop through the whole array and returns the first element that satisfied the
condition;
Return: a single value
Time Complexity: BIG O(n)

findIndex: This is very similar to Find method. But this doesn’t return the satisfied element, rather than it returns the
index of that element.
Return: index of satisfied element;
Time Complexity: BIG O(n)

some: Some is also a method of array. This method returns only a boolean value. callback function go through all the elements
and check if any of the elements fullfill the condition. If does, than this function return true. If not, than it returns
false.
Return: boolean value;
Time Complexity: BIG O(n)

every: Every is a vary similar to some method. But it return true if all those elements satisfied the given condition, Or it
will result false;
Return: boolean value;
Time Complexity: BIG O(n)

push: Push method add a new Element at the end of the array;
Return: it mutate the original array;
Time Complexity: it add element at the end of the array, So it’s time complexity is BIG O(1)

unshift: This Array method add element at the beginning of the array.
Return: it mutate the original array;
Time Complexity: As it add element in the beginning, for this we have to change the index of rest of the elements. So time
complexity of this method is BIG O(n)

indexOf: This Array method takes an input and check whether is this array has any element that matches the given input.
if it matches than this will result the index of that element. If it doesn’t find any element, than it will return -1.
Return: index of given element or -1;
Time complexity: BIG O(n)

Thanks for your time! :)

--

--