Arrays
Answers to frequently asked questions on how to use Arrays in Javascript.
Efficient Coding of Loops
Sometimes a small change to your code can make a big difference to how fast it runs. Here are a couple of changes you may want to change your for loops in order that they can process arrays quicker.
Sometimes a small change to your code can make a big difference to how fast it runs. Here are a couple of changes you may want to change your for loops in order that they can process arrays quicker.
Array Maximum and Minimum Values
Extending your arrays to add methods to get the maximum and minimum values that they contain.
Extending your arrays to add methods to get the maximum and minimum values that they contain.
Array Average Value
Here's a method that you can add to your arrays that will retrieve the numerical average of all the numbers in an array.
Here's a method that you can add to your arrays that will retrieve the numerical average of all the numbers in an array.
Stacks
One construct that Javascript does not at first glance appear to support is that of the Stack. Here's how to use an array to simulate a stack.
One construct that Javascript does not at first glance appear to support is that of the Stack. Here's how to use an array to simulate a stack.
Queues
Arrays can also be used to simulate queue constructs.
Arrays can also be used to simulate queue constructs.
Numeric Array Sort
The sort method normally sorts Javascript arrays in dictionary order. If your array contains numbers then you will want to sort numerically instead. Here's how.
The sort method normally sorts Javascript arrays in dictionary order. If your array contains numbers then you will want to sort numerically instead. Here's how.
Ignore Case Array Sort
The default sort sequence has uppercase letters first followed by lowercase letters. Here's how you can tell the sort to ignore case.
The default sort sequence has uppercase letters first followed by lowercase letters. Here's how you can tell the sort to ignore case.
Random Array Sort
If you want to be able to select two or more from a selection of entries then one way to do it is to "sort" your array into random order.
If you want to be able to select two or more from a selection of entries then one way to do it is to "sort" your array into random order.
Date Array Sort
Sorting an array of dates in yyyy/mm/dd order is simple. That doesn't mean that sorting an array of dates in mm/dd/yyyy or dd/mm/yyyy order is hard. You just need to set up the right compare routine.
Sorting an array of dates in yyyy/mm/dd order is simple. That doesn't mean that sorting an array of dates in mm/dd/yyyy or dd/mm/yyyy order is hard. You just need to set up the right compare routine.
Associative Arrays
Javascript supports two different types of arrays - those where the entries are identified by number and those where the entries are identified by name. The latter are called Associative Arrays.
Javascript supports two different types of arrays - those where the entries are identified by number and those where the entries are identified by name. The latter are called Associative Arrays.
Multi Dimensional Arrays
Javascritp doesn't actually support multiple dimensional arrays. Instead it allows you to put any element type into each array element which includes arrays.
Javascritp doesn't actually support multiple dimensional arrays. Instead it allows you to put any element type into each array element which includes arrays.
Lotto Picker
A demonstration of how just four array methods can be combined together to select a specified number of unique entries from a larger group just as you have to do when playing lotto (or pools or lottery or whatever they call it where you live).
A demonstration of how just four array methods can be combined together to select a specified number of unique entries from a larger group just as you have to do when playing lotto (or pools or lottery or whatever they call it where you live).
