Monday, June 29, 2015

JavaScript Every vs. Some Array Methods

JavaScript arrays comes with methods that behave like the And && and Or || operators.

The every method takes a function with each array element as parameter. The function tests each array element, and if the test of all array elements evaluate to True, the method returns a true value. If at least one test evaluates to false, the method now returns a false value. The method behaves very much like expressions having And && operator, wherein the expression will only return true if the evaluated expression to the left and to the right evaluates to true.

The some method also takes a function with each array element as parameter. However, the function only needs to determine if at least one array element evaluates the test to be true. The method therefore behaves similar to expressions having the Or operator, wherein the either expression to the left of right needs to evaluate to true for the operator to return true.

In the sample the code snippet below, the inventory of PC issued to each employee logs how much RAM and HD memories are installed.

<script>
    var units =[{name: "Ernie", GBRAM: 1024, GBHD: 512000}, {name: "Bert", GBRAM: 2048, GBHD: 512000}, {name: "Kermit", GBRAM: 3072, GBHD: 512000}];
    var checkMem = function(u){
        return u.GBRAM > 1024;
    }
    console.log("Employees PC Units Inventory..");
    units.forEach(function(p){console.log(p.name)})
    console.log("All Units having more than 1 GB RAM",units.every(checkMem));
    console.log("Some Units having more than 1 GB RAM",units.some(checkMem));
</script>

An array units contains objects having name, GBRAM and GBHD as keys. The name key identifies the employee. The GBRAM  and GBHD records how much Giga Byte memory is installed in the RAM and HD.



The line unit.forEach() goes through each array element without having to use any of JavaScript loop control. The method takes a function as a parameter. The function then takes each unit array element and passed to its defined parameter p. Each array elemnt is an object with key value pairs.The function logs the value of the name key in the array element object.

The output is show below:

The function checkMem will accept an element object from the array unit. The test if the object GBRAM key is greater than 1024 is returned.

Array calls to every and some methods are made, wherein both method takes the function checkMem as the parameter. Both every() and some() methods will go thru each element object and pass it to checkMem.

The array elements to be passed to checkMem are shown below:
Checking each array element object, both every() and some() methods will evaluate the first element object (name: "Ernie") to be false, but will evaluate the rest to true.

Since every() method returns true only if all array elements evaluated in its function parameter is true, then the method will not return true. On the other hand, some() method only needs one array element to evaluate to true in the function test, it will return a true value. The complete console output is shown below:

The full code can be accessed on the link below:
https://github.com/jundolor/JavaScript-Every-vs-Some/blob/master/index.html