JavaScript array methods may be combined to generate a result. The code study in this post will filter selected array elements and summarize the data. The JavaScript array methods that will be combined are filter() and reduce().
The array which will be the subject of the code study is shown below:
Each element of the array is an object with key-value pairs. The idea of the array is to provide information on computers that have been issued to various persons., The name key identifies the person, GBHD stands for Giga Byte HD- the total capacity of the HD, GBHDFree stands for Guga Byte HD Free-meaning the free space on the HD, and OS-the operating system of the computer
Using method filter(), as shown in the code snippet below, the array is narrowed down to computer units with MacOS as the operating system:
<script>
var units =[
{name: "Ernie", GBHD: 500, GBHDFree: 300, OS: "Windows"},
{name: "Bert", GBHD: 800, GBHDFree: 350, OS: "MacOS"},
{name: "Kermit", GBHD: 600, GBHDFree: 380, OS: "Windows"},
{name: "Oscar", GBHD: 450, GBHDFree: 200, OS: "Windows"},
{name: "Grover", GBHD: 700, GBHDFree: 300, OS: "MacOS"},
{name: "Count", GBHD: 700, GBHDFree: 250, OS: "MacOS"}
];
var filterOS = function(u){
return u.OS == "MacOS"
}
console.log("MacOS Units:" ,units.filter(filterOS));
//MacOS Units: {name: Bert...}, {name: Grover...}, {name: Count...}
</script>
The method filter() calls the function filterOS(). Each array element, an object is passed to the parameter. The function returns back the object has "MacOS" as the value to the OS key. All other objects are filtered out. An array of objects with key-value pair of OS: "MacOS" is returned. Note that the original array is retained.
The method reduce(), as shown in the code snippet below calls the function mostHDFree(). The function, in turn, goes thru each array element, an object, to see which has the biggest GBHDFree value.
<script>
var units =[
{name: "Ernie", GBHD: 500, GBHDFree: 300, OS: "Windows"},
{name: "Bert", GBHD: 800, GBHDFree: 350, OS: "MacOS"},
{name: "Kermit", GBHD: 600, GBHDFree: 380, OS: "Windows"},
{name: "Oscar", GBHD: 450, GBHDFree: 200, OS: "Windows"},
{name: "Grover", GBHD: 700, GBHDFree: 300, OS: "MacOS"},
{name: "Count", GBHD: 700, GBHDFree: 250, OS: "MacOS"}
];
var mostHDFree = function(start, current){
if(start.GBHDFree > current.GBHDFree) return start;
else return current;
}
console.log("Unit with most HD free space",units.reduce(mostHDFree));
//Unit with most HD free space {name: "Kermit", GBHD: 600, GBHDFree: 380, OS: "Windows"}
</script>
The methods filter() and reduce() may be combined to determine what objects with key-value pair of OS: "MacOS" has the most GBHDFree value.
JavaScript starts with the leftmost method and soon as a result is generated, calls the next method to the right, passing the generated result. The cycle will continue for additional methods to the right.
In the case of the filter() and reduce method() combine, the filter() returns an array of objects with OS: "MacOS". The array is passed to the reduce() method which selects the object with the most GBHDFree value.
The code snippet is listed below:
<script>
var units =[
{name: "Ernie", GBHD: 500, GBHDFree: 300, OS: "Windows"},
{name: "Bert", GBHD: 800, GBHDFree: 350, OS: "MacOS"},
{name: "Kermit", GBHD: 600, GBHDFree: 380, OS: "Windows"},
{name: "Oscar", GBHD: 450, GBHDFree: 200, OS: "Windows"},
{name: "Grover", GBHD: 700, GBHDFree: 300, OS: "MacOS"},
{name: "Count", GBHD: 700, GBHDFree: 250, OS: "MacOS"}
];
var filterOS = function(u){
return u.OS == "MacOS"
}
var mostHDFree = function(start, current){
if(start.GBHDFree > current.GBHDFree) return start;
else return current;
}
console.log("MacOS with most HD space free",units.filter(filterOS).reduce(mostHDFree));
//MacOS with most HD space free {name: "Bert"...},
</script>
The full code can be downloaded at:
https://github.com/jundolor/JavaScript-Combine-Array-Methods/blob/master/index.html