015_Deep Dive into Array Method
Working with mutator and immutator array methods:
- push,pop,shift,unshift,splice,isArray and soon are mutator methods
- slice,join,concat, and other soon are immutator methods
Adding an Item to an Array
- If we want to add a new item to the array, we can assign a value to the next index.
- When we want to remove a specific item from an array, we use the
splice()method - only available on real array
- takes two arguments only
- returns new brand array and also nice way of copying array.
- syntax : slice(start , end) // end is excluded
- can be used in select range.
- using old array and creating new array.
Looping Through an Array
We can loop through the entirety of the array with the for keyword, taking advantage of the length property. In this example, we can create an array of shellfish and print out each index number as well as each value to the console.
Output0 'oyster'
1 'shrimp'
2 'clam'
3 'mussel'
We can also use the for...of loop, a newer feature of JavaScript.
Outputdolphin
whale
manatee
Note : The for...of loop does not retrieve the index number of the elements in the array,
but it is generally a simpler, more concise way to loop through an array.
Using loops is extremely useful for printing out the whole value of an array, such as when displaying the items from a database on a website.
concat(): let testResults = [1, 2, 3, 4, 5, 6, 7, 8];
- copies new array hence creating new place in memory.
Reteriving index in javascript: indexof() and last index of()
console.log(testResult.indexOf(6)) // return index 4
Note : searching from the left to right and if more than one times with same value,it
stop after finding first match .
- works well for primitive value but not for reference value.
- if no match found returns -1
lastIndexOf()
The lastIndexOf() method returns the index number of the last instance of an element.
We can test on the same example from indexOf(), which includes barracuda twice.
Output3
lastIndexOf() will search the array starting from the end and return the first index number it finds
Arrays are similar to strings, in that they both consist of a sequence of elements that can be accessed via index number. However, it is important to remember that strings are an immutable datatype, meaning they cannot be changed. Arrays, on the other hand, are mutable, which means that many array methods will affect the original array, not a copy of the array.
Note: Array methods are properly written out as Array.prototype.method(), as Array.prototype refers to the Array object itself. For simplicity, we will simply list the name as method().
isArray()
Before we get into mutator methods, let’s look at the isArray() method to test whether objects are arrays. This is a Boolean method that returns true if the value of a variable is equal to an array. If the object is not an array, this method returns false.
Outputtrue
The isArray() method is useful because the typeof operator we would normally use for testing returns object when used with arrays, and sometimes knowing the distinction between an object and an Array object is necessary.
Note that isArray() is written differently from most array methods, with the array variable being provided as an argument to the method.
reverse()
The reverse() method reverses the order of the elements in an array.
Using reverse(), the last element will be first, and the first element will be last.
Output[ 'eel', 'koi', 'barracuda', 'piranha' ]
The reverse() array method has no parameters.
fill()
The fill() method replaces all the elements in an array with a static value.
In the fish array, we have four items. Let’s apply fill().
Output[ 'shark', 'shark', 'shark', 'shark' ]
All four items in the array have been replaced with the same value, "shark". fill() also takes optional arguments of start and end points.
Using fill() we can replace one or more elements in an array with a static value
join()
The join() method converts all the elements of an array into a new string.
If no argument is given, the output of join() will be a comma-separated string with no extra whitespace.
Output'piranha,barracuda,koi,eel'
In order to include whitespace or another separator, you can add a string of your separator as a parameter to the join() method. This parameter will contain the separator you would like between each array element.
Output'piranha, barracuda, koi, eel'
In the above example, writing ', ' with whitespace separated the array items in a more readable fashion. An empty string provided as an argument will remove the default commas completely.
- using seperator as blackslash
sort()
The sort() method sorts the elements in an array based on the first character in the element. In the case that the first character is identical, it will continue down the line and compare the second character, and so on.
By default, sort() will alphabetize an array of strings that are all either uppercase or lowercase.
Output[ 'barracuda', 'eel', 'koi', 'piranha' ]
Since sort() is based on the first unicode character, it will sort uppercase items before lowercase.
Let’s modify our original array so that one of our strings begin with an uppercase letter.
Output[ 'Koi', 'barracuda', 'eel', 'piranha' ]
Numbers come before both uppercase and lowercase characters.
We can again modify the array to include a number in one of the string items.
Output[ '1 eel', 'Koi', 'barracuda', 'piranha' ]
sort() will not sort an array of numbers by size by default. Instead, it will only check the first character in the number.
Output[ 15, 16, 23, 4, 42, 8 ]
In order to sort numbers properly, you could create a comparison function as an argument.
Output[ 4, 8, 15, 16, 23, 42 ]
The sortNumerically comparison function allowed us to sort as intended. sort() will apply the change to the original array.
Wap to reverse a number:
Comments
Post a Comment