These methods are also referred to as array functions or just array functions. The methods are standardized by ECMAScript, which means they are supported by all modern browsers except Internet Explorer versions 8 and earlier. They can also be supported by other web standards like HTML5, although it depends on how you write your code.
Through learning array methods we create a strong base for our front-end development work. And it’s good to know what all these different methods do because we will be getting familiar with them all throughout our journey in learning Angular 2/4 development on front-end technologies as well! In case you missed my previous post where I went over array manipulation check that out HERE! With that said, let’s jump into some more array fun!! ❤.append(): The .append() method adds an element or elements to an array after any existing elements and returns a reference to the modified array. Example:
[John, Mike, Sara] .append(Jane); //Outputs [John, Mike, Sara, Jane]
Now just one thing before you go ahead and start using append —make sure there are no duplicates in your current list. If there are duplicate values then they won’t get added until they’re sorted by their indices, which can also cause an error if they aren’t properly sorted then they get added into your final output! This isn’t a huge deal but it’s something to keep in mind when working with arrays on front-end development!
__.filter() : The .filter() method creates a new array with all elements that pass test implemented by the provided function, reusing existing filter rules for matching elements. Example: [1, 2, 3] .filter(x => x > 1) //Outputs [2, 3] You might have noticed that I used => instead of = earlier here; it’s because I’m showing how it would be written without destructuring assignment, as per my previous post HERE ! It works either way though so you could use whatever syntax seems easier for you to understand!
Before diving into an array method, first, let’s define what we mean by an array method. An array method is a generic function that accepts an array as its input and returns another array. The output of one array method can be used as input to other methods or functions.
But you can also apply array methods to actual arrays. In most cases, it doesn’t matter whether or not you call them on wrappers or actual data types. Call them however feels most comfortable. What does matter is that whenever you pass an array as input to a method, whatever it returns will always be an array too! That means that regardless of whether we’re dealing with data types such as strings, objects, functions, etc…we can keep writing our code without ever having to cast anything between different data types! Well-defined conventions emerge naturally when working in a language where concepts aren’t tied to implementation details such as data type wrappers.
Plus, functions don’t evaluate their expressions until they’re executed (see hoisting), so when we create functions inside loops, it executes them multiple times but only gives them values from different iterations once (anonymous functions created outside loops always execute only once per iteration). Bypassing these new values into our inner functions every iteration, instead of just once at a certain index like other methods do, forEach() also allows us to modify any part of our arrays by returning those modified values into them. That means whatever tasks these inner functions perform can affect all of their items instead of just their indexes.
In math terms, reduce would take 4 + 5 = ? and return 9. This can be done by adding two numbers together (the addition operator (+)) or multiplying numbers together (the multiplication operator (*)). sort(): Returns a sorted version of an array. You might use sort to alphabetize or otherwise reorder items in your application’s menu system, for instance. unshift() / shift():
The unshift method appends one or more elements to an array; it behaves identically to push(), except it does not alter its argument. The shift method removes and returns the first element from an array, shortening it by 1. Note that arguments cannot be empty when either unshift ()or shift ()is called.. pop() / splice(): Both pop() and splice() remove an element from an array. Unlike shift(), both also let you specify where to delete the item being removed—meaning a different item is removed from each position within arguments . Try using these three pop examples: once (), last (), and random ().