For a basic example of an array there is starting out bu just creating one by using the Array constructor, or the array bracket syntax. I will be getting into detail about the various ways of going about creating an array in a later section in this post, but for now there is just starting out with a quick example that uses the bracket syntax.
Once an instance of an array is created there are a number of things that can then be done with the array. Prototype methods such as the array jon method for example can be used to create a string value of the array, there is also accessing the length property of the array. In order to confirm that the instance of an array is indeed an array that is using the instance of operator, or checking the name property of the constructor object of the array.
There are a number of useful methods to work with in the prototype object of an instance of an array. One example of this would be the array map method that will return a new array with elements that are result of some code that is in a function that is called for each element in the source array.
There is a great number of additional method to be aware of that I will be getting into in depth later on in this post. For now there is just being aware of the fact that there are a whole lot of tools in the array tool box of sorts, and methods like map, join, and for each are just a few of these tools.
Anther way to create an array is to use the array literal syntax which is my usual go to way to making one to begin with. This involves the use of square brackets, and placing starting elements in between commas that lay between the opening and closing square brackets. In addition an empty array with a zero starting length can be created by just not giving any starting elements, or in other words just having and opening and closing set of square brackets.
Square brackets are also used as a way of getting a single element in an array also by using them off the end of an array and having an index value between the opening and closing square brackets. So then this square bracket syntax can be used to not just create and array, but also be used to reference a given index value as well while we are at it. On top of this square brackets can be used to get at any key of any object or collection in general, so this can also be used to get a named key of a plain old object also.
Another way of creating an array, or end up with one rather, would be to call a method that would return one as a product. In some cases I would like to not create and array from scratch by rather create one from some kind of data source such as a string. In the string prototype object there is the string split method that can be used to split a string into a bunch of substrings where each substring is an element in a resulting array that is returned. By default the delimiter use by this method is a comma, but any static value can be used in various kind of situations, for example a space can be used if I wan to split text by words, and an empty string can be given if I want to split a string into an array of letters.
2.4 - The Object.keys and Object.values methods can be used to create arrays of key names or values from Objects
Often I might want to create an array of key names or values of an object. So on top of using things like the array constructor and bracket syntax there is also using static Object methods as a way to go about creating an array that is populated with values that I want right away. Using a method like Object.keys saves me the trouble of creating an empty array and then using something like a for in loop as a way to loop over the keys of an object and push key names into such an array.
There is then the array from static method of the Array global that is yet another way to go about creating an array. What is great about this method is that it is a nice way to go about quickly creating an array from an array like object. That is a plain old object ir any object that is formated like an array, but because of the way the object was created is not an instance of array. Feeding that kind of object to the array from method will return a new array that is an instance of array created with the properties of this array like object.
There is a great deal more to write about when it comes to array like objects, so there is another section later in this post in which I revised this subject in further detail. For now as far as this section is concerned this is just yet another way of how to go about creating an array.
One of the most simple ways to go about adding elements to an Array is to just use the square bracket notation to set the element value of a desired index. To do this just use square brackets after the variable name with the desired index value passed via the brackets. This can be used as a way to both set, and query a certain index value of the array. For example I can use a while loop to just step an index variable and then use that index variable with the bracket syntax to start setting element values of an array.
There are a number of other ways to add or set element to an array rather than just using a while loop though. For example I can create a function that uses an expression to figure what an index value should be for a single element that I want to add or change
The Array.push method can be used to add one or more additional elements to the end of an Array. This is often used in place of using the length property of an array as a means of finding out what the current index value is for a new element that will be appended to the end of an array.
The unshift method works just like the push method but it can be used to add one or more elements to the beginning of an Array at index 0, rather than at the end of an array. So then that is push to add an element at the end, and unshift to add an element to the begging of the array at the left most position. Also just like that is the push method it is possible to add more than one element at a time, but just making use of more than one argument when calling the method.
So there are a number of useful Array prototype methods that can be used such as the Array.concat method that can be used to add together two ore more arrays into a single array. In addition there is also the Array.slice method that can come in handy when I want to get an array segment from an existing array from a given starting and ending index.
3.5 - The Array.splice method can be used to remove no elements, and inject ones at an index location
The array splice method is typically used as a way to remove elements from an array. However while removing elements it is also possible to use the method to add some elements in at an index location also. In addition to this option to add elements to the array it is also possible to give a value of zero for the number of element to remove at an index location. So then the array splice method can be used to just add elements to an array and do so at any index location.
So because Arrays are still objects they can still be used in the same way as I would a plain old object created with the Object constructor, or the curly bracket syntax. So if I want to I can just add any additional public properties to an array of I want to in terms of named keys for the array, such as a method that will add up all the numbers of an array just for the sake of an examples of this.
An example of an Array like object would be the arguments object of function. Whenever a function is called, inside the body of a function there is an arguments object that contains the given arguments to the function. This is useful for defining different logic that is to be used depending on the number of arguments that are given to the function. However for the sake of the content of this post, and this section what is of interest here is the way that the object is structured.
So because the argument objects constructor is Object and not Array it is just a plain old Object, and as such it does not have Array prototype methods available to it. However it is still very mush structured like an Array so it can be considered an Array like Object.
Just like with the arguments object in a function these objects are structured just like an Array, but because they are not an instance of Array they do not have Array prototype methods like Array.forEach or Array.filter
If an Object that is not an instance of Array is still structured just like an Array where the keys are index values, and there is a single length property that is the number of elements. Then it is possible to still get Array prototype methods to work just fine with these by making use of something like Function.call, Function.apply, or Function.bind.
These function prototype methods will come up often as they are a way to break a prototype methods away from its class and get the method to work with any kind of object given that it is formated in a way that will allow it to work with a prototype method. There are a whole lot of other reasons why I would want to use one of these function prototype methods, but getting into it in detail here might prove to be a little off topic from arrays in general.
This works because a positive number evaluates to true, and a number of zero or lower does not. So I can set a number to the length of an array, and subtract from that number as well in the same location. When the index value reaches a value of zero the loop will end. I have nothing against for loops, and I am not going to say this is the best and only way to loop over an array. However it does has its good points.
One of the most important tasks to preform with arrays is to filter them to create a new array that is a kind of sub collection of elements. One of the many Array prototype methods is Array.filter than can help with filtering tasks. The Array.filter method creates a new array rather than mutating the Array that it is call off of making Array.filter a functional programming friendly method.
To use Array.filter all I need to do is just call the method, and pass a function that will be used to filter the Array that I call filter off of. The current element is passed as the first argument which I can use when defining the logic that will be used to filter. If a true boolean value is returned the current element will be included, else it will not.
Another handy Array method to know about is of course Array.map. This method is useful when I want to apply some kind of logic to all elements in an array, and remap the contents in the process. This is another array method that excepts a function as its first argument like that of Array.forEach, or Array.filter. However this time the returned value is what will be set as the current index value.
In lodash there is the _.map method that works the same way as Array.map, but it is a collection method so it can be used with objects as well as a means of mapping both array elements, and objects keys in general.
When it comes to working with an array there will come a time where I will want to find something in the array. This can have more that one meaning, but will often mean finding a single element in the array. There is some over lap though when it comes to finding something in an array, filtering and array, and sorting an array though.
There is the array find method that can be used to find a single element in an array that meets a condition of some kind given with a function. This works by applying the callback for each element starting from index 0 forward to the end of the array. In the event that the function that is given to the find method returns true for a given element, that element value will then be the returned value from the call of array kind.
Here is a basic example of using the sort array prototype method on an array or primitives. When it comes to using the array sort method this way I do not even have to give a sort function as the default functionally will work well when it comes to that. One thing to point out right away with this is that the array sort method will mutate the order if index values in place. So if I do not want that to happen I will need to make a copy of the array first. If I do not want the numbers to be in the order of smallest to largest I can use the array reverse method, or get into making a custom sort method.
The reduce method is often used for tasks that involve condensing an array into a single primitive value such as a number or a string. However the method can also be used as an alternative to the array filter method, and even though the method is called reduce it can also be used to create a larger new array from a source array actually depending of course on the logic in the function that is passed to it.
The for each method is a prototype type method that can be used as one of several options for just looping over the contents of an array. As I have covered in the above section on looping I generally prefer to use while loops as a way to loop over an array in some kind if general custom way. There are a number of reasons why I often do go with some other prototype method in place of the for each method also, as there are many other prototype methods that are similar to that of for each, but prove to be a better choice for certain kinds of tasks. Still I find myself using for each on occasion when it comes to quick simple examples and solutions, it is just not by any means the only way to go about doing this sort of thing.
The array every method is one of two methods that have to do with testing if all or some of the elements in an array meet a given condition or not. The every method is the method that will only return true of all elements in the array meet a given condition while the some method is what will return true of just one element in the array meets the given condition. For example if I want to find out of all the elements in an array are numbers or not I can use the array every method and use the type of operator in the expression that will be used to create the return value of the function that I give to the array every method.
The array some method is then similar to that of the array every method, but it will return true if just one element in the array meets a given condition.
So I do not have to have an array of arrays, but just a simple plain old linear array. So this can be thought of as a kind of virtual multidimensional array, because it is just a linear collection of elements. When doing this an expression can be used as a way to get of set the proper index in the array.
The flat method that I covered in the section on prototypes will then come into play when it comes to working with multidimensional arrays. That is that if I have an array of arrays, but I want to flatten that into a form like that of the linear array from that I tend to prefer, then I am going to want a flatten method. The thing about the flat array prototype method is that it is still relatively new in the grand scheme of things, if this proves to not be a problem with the clients that you care about then maybe one can just use the flatten method and move on. If it is a problem then there is using some kind of user space solution in place of the array flat method, with that said when it comes to lodash there are a few methods that have to do with flattening an array. There is also looking into one or more options when it comes to poly-filling this native array method.
Another subject of arrays is what is often referred to as the length of an array to which I will not be getting into in detail here as I have all ready went off the deep end when it comes to that topic. The length of an array is just a property of an array object that contains a number that is the current max element size of an array, but with that said yes the length can change. The length of an array is often confused with other values of an array, such as the number of actual public numbed keys, or elements that there are in the array. So it is worth looking into playing around with a few quick code examples to have a better understating of what the deal is with array length, and that this is not always reflective as to how many elements are in the array, depending on how you go about counting elements.
First off there is just knowing what the length property is about when it comes to a typical basic situation at least. If I have an array that I create with the array bracket syntax then the length of that array is 1 as one might expect.