This blog post will cover the concepts of Arrays in JavaScript and different ways to manipulate Arrays in JavaScript.
Introduction
Objects normally allow to store keyed collection of values, but when it is required to store ordered collection, then JavaScript will come into the picture. JavaScript Array is a special variable, which can hold more than one value (of different types) at a time.
Declaration
There are basically two syntaxes for creating an empty array.
//Option 1
varnames = [“James”, “Robert”, “Shane”];
//Option 2
varnames;
names = [“James”, “Robert”, “Shane”];
Navigation
Below are the different ways to iterate through the array using indexes.
varnames = [“James”, “Robert”, “Shane”];
//Option 1
for (leti = 0;i <names.length;i++) {
console.log(names[i]);
}
//Option 2
for (letsingleName ofnames) {
console.log(singleName);
}
//Option 3
for (letindex innames) {
console.log(names[index]);
}
//Option 4
names.forEach((singleName) => {
console.log(singleName.toUpperCase());
});
Finding Array Elements
Below methods are mainly used to find elements in Array.
includes()
This method will return true if the specified value is present in the array, otherwise, it will pass false.
mergedCountry: India,Canada,Singapore,Switzerland,United States of America,Norway
join()
This method can be used to join all the elements of the array separated by commas or the separator specified. It will return a new string without modifying the original arrays.
joinedCountry: India <=> Canada <=> Singapore <=> Switzerland <=> United States of America <=> Norway
Sort Arrays
sort()
This method can be used to sort elements of an array. By default, it will do the sorting in ascending order, but an optional parameter can be used to define the sort order. To understand sort method in details, please read the post – Sorting Arrays in JavaScript
This method will map each element of the array to something else. This method will require a callback function that will take three parameters (currentValue – this is mandatory, index and array). Finally, it will return an array.
employeesInUpperCase: JAMES BOND,JACKY JONES,KELLY DITNER,JONATHAN PATEL
filter()
This method will test each element of the array with the filter criteria. This method will require a callback function that will take three parameters (currentValue – this is mandatory, index and array). Finally, it will return an array.
This method will work on each element of the array and reduce it to one single value based on the logic written in the callback function. This method will require a callback function that will take two parameters (accumulator and currentValue), then the second parameter is the current index or initial value of the accumulator and the final parameter is the array. Finally, it will return an array.
This method will work on subarrays and flatten them to make them a single array. You can specify the depth of flattening as an argument for the flat method.
This method will work on every element of the array and will return true or false based on whether at least one element passes the condition mentioned.
This operator allows an iterable such as an array expression or string to expand where the number of arguments expected is dynamic. It can be 0, 1, or 100. It is mostly used in variable arrays.
Rest is the way to handle function parameters, allowing more easily to handle various inputs as parameters in the function. The syntax is the same as spread.
Wow, marvelous weblog structure! How long have you been running a blog for? you make running a blog look easy. The entire look of your website is great, as well as the content!
I’ve read some just right stuff here. Certainly value bookmarking for revisiting. I surprise how a lot attempt you place to create one of these excellent informative web site.
I am extremely impressed together with your writing skills as well as with the structure in your weblog. Is that this a paid theme or did you modify it your self? Either way stay up the nice high quality writing, it’s rare to peer a great weblog like this one these days..
This is really interesting, You are a very skilled blogger. I’ve joined your rss feed and look ahead to in search of more of your great post. Additionally, I have shared your web site in my social networks
Amazing! This blog looks exactly like my old one! It’s on a totally different topic but it has pretty much the same layout and design. Great choice of colors!
Wow, marvelous blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is wonderful, let alone the content!
Excellent post. Keep posting such kind of information on your site. Im really impressed by your site. Hey there, You have performed an excellent job. I will certainly digg it and personally recommend to my friends. I’m confident they will be benefited from this site.
Wow, marvelous weblog structure! How long have you been running a blog
for? you make running a blog look easy. The entire
look of your website is great, as well as the content!
I’ve read some just right stuff here. Certainly value bookmarking for revisiting. I surprise how a lot attempt you place to create one of these excellent informative web site.
Excellent site. Lots of useful info here. I’m sending it to a few buddies ans also sharing in delicious.
And certainly, thank you for your effort!
Do you have any video of that? I’d love to find out some additional information.
I am extremely impressed together with your writing skills as well as
with the structure in your weblog. Is that this a paid theme or
did you modify it your self? Either way stay up
the nice high quality writing, it’s rare to peer a great weblog like this
one these days..
very interesting, good job and thanks for sharing
such a good blog.
Thank you sharing for this great post.
This is really interesting, You are a very skilled blogger.
I’ve joined your rss feed and look ahead to in search of more of your great post.
Additionally, I have shared your web site
in my social networks
Amazing! This blog looks exactly like my old one!
It’s on a totally different topic but it has pretty much the same layout and design.
Great choice of colors!
Wow, marvelous blog layout! How long have you been blogging
for? you make blogging look easy. The overall look of your web site is wonderful, let
alone the content!
Excellent post. Keep posting such kind of information on your site.
Im really impressed by your site.
Hey there, You have performed an excellent job. I will certainly digg it and personally recommend to my
friends. I’m confident they will be benefited from this site.
Greetings! Very useful advice within this article! It’s the little changes that will make the most
significant changes. Thanks a lot for sharing!
Thanks for sharing such a pleasant opinion, post is pleasant,
thats why i have read it fully