Transform an Array
Search in Array
Modify in Array
Loop the array
Check the array
20) some()21) every()
Map()take the array and convert it into a new array with the result of calling a function for every item in the array.
In the following example, we get an array which includes the number of letters of each country names by using
Map() function. It takes a single parameter “
Click to see the Example
(i) map() creates a new array and it doesn’t affect the original array.
(i) map() will not execute if there are no items in the array.
sort()allows you to sort the items in the array in ascending order. This works well for string values in the array and not for numbers. But you can do it for numbers using compare function.
Here, I sort all the cities in the array to ascending order.
(i) This changes the original array.
(ii) Provide inaccurate results for sorting numbers.
CompareFunction – // TODO
reverse()allows you to reverse the order of items in the array.
Here, I sort the array in descending order.
reverse() changes the original array.
join()method allows you to join the all elements of an array in to a string by a seperator (default seperator is comma)
join()method will join all
cities of the array using a comma and return it as a string.
Reduce the array to a single value and execute the provided function for each value of an array from left to right.
So if we wanted to get the total population of all the different countries in this array, normally we would use a
for loop and add the population of every single time and at the end of the
for loop, you would print out the price but you can use the reduce method to do this instead.
It doesn’t change the original array and the return value of the function is stored in an accumulator (
Since it’s actually doing some operation on the array and returning a combination of all those different operations.
indexOf() method search the specific item you pass as an argument, and it returns its index position. Search from start to end.
"Delhi" is repeated for 2 times and I want the index position of first
"Delhi" element in the array.
lastIndexOf() method also search the specific item you pass as an argument and return its position. If that particular item presents more than once, it returns the position of the last index.
"Delhi" is repeated for 2 times and I want the index position of last
"Delhi" element in the array.
includes() method check whether the array contains you pass as an argument. It returns true if contains, and false if not.
Delhi exists in the array.
find()allows you to find a first single element in an array that passes the condition inside the function.
Let’s say we want to get the country with the name of
'India’, So we can check
’India’. This is just going to return the very first item that it finds in the array that returns true for the statement that you pass inside of this
find() doesn’t check the remaining values if it finds the array element where the function returns a
true. If it couldn’t find any element, it will return
filter()method filter all array elements that pass the condition inside the function and returns a new array.
Let’s assume that we want to get all the items on this list that are greater than 50 million population. So all we would need to use is the
filter() method to filter out everything.
The great thing about
filter() method is that it doesn’t change the underlying array (
findIndex()method returns the first element of the arrray that passes the condition for the provided function.
push()method allows us to add a new item at the end of the array and it returns the new array length.
Following array contains the list of sports and I add another sport
"rugby" at the end of the array.
pop()method is used to remove the last element of the array, and it returns the removed item.
Here, we use
pop() method to remove the last element
"Netball" from the array.
shift()method is used to remove the first item of the array, and it returns the remove item.
Following array contains the list school facilities and use
shift() method to remove the
"Laboratory" facility from the beginning of the array.
unshift()method is used to add a new item to the beginning of the array, and it returns the new array length.
"schoolFacilities" array, use
unshift() method to add 2 more facilities.
splice()method is used to add or remove mulitple items to the array and it returns all removed items. Original array will be changed.
"jsFrameworks" array, if you want to add 2 more other JS frameworks to the index
2 and no need to remove items after index 2, do like this,
slice()method returns the selected items in an array and return them in a new array.
"jsFrameworks" array, if you want to slice first and second elements, use
slice() method and pass the index of first and second elements as below.
concat()method is used to join 2 or more arrays and it returns a new array with those values of joined arrays.
Here, we have 3 arrays and need to join all of them and convert it to a string value. So use
concat() method will do it as below,
foreach()calls a function for each element in the array and does not return anything. Very similarly to a
foreachtake a function instead.
We have our first parameter
country just like before and we could use any attribute in
country object for this below example.
This makes working with arrays where you need to loop over them easier since you don’t have to write out the clunky long for loop syntax as you’d normally have to.
sum()checks any element in the array return a
truefor the condition inside the function. If a single item returns
true, it’s going to return
truefor the entire thing.
For example, we can check if some of the items in this array have a population of less than 50 million.
every()method check if all values in the array meet the condition true for provided function and if it finds any element returns a
falsevalue and then
every()method will also return
Let say, if we want to check all the population in each country has more than 20 million,
Every() method will be a better option.
It does not check the remaining value. It will return
true, if no false occur for each element.
Cool Gadgets on Amazon 2019 | Cool Amazon ProductsG, 0 Comments Today’s, topic about Cool Gadgets on Amazon 2019. Now, let's go and know about those gadgets in more deep. 01. The orange screwThis is one of the most clever and versatile tools you'll ever...
Is Interstellar Travel Impossible? | Interstellar TravelG, 0 Comments Today's, topic about Interstellar Travel Impossible. Let's go into deep and understand the scientific facts related to this topic. Simply, Is interstellar travel doomed to remain in the...
Smart Retail Technology in IoT – Smart Retail StoresG, 0 Comments Today's topic is about "How IOT Gives Rise to Smart Retail Technology". Simply, I want you to picture your kitchen at home. If you're like most people you can trace many of your...