DEV Community

param-19
param-19

Posted on

Higher Order Functions

Abstraction

Abstraction is the process of hiding information from the users. They give us the ability to understand and read code at a higher level, while hiding the details, to give us an overview.

  • There exists a filter method which builds a new array that contains the results that pass given test.
console.log(filter(SCRIPTS, script => script.living));
// → [{name: "Adlam", ...}, ...]
Enter fullscreen mode Exit fullscreen mode

Maps

The map method transforms an array by applying a function to all of its elements and building a new array from the returned values. The new array will have the same length as the input array, but its content will have been mapped to a new form by the function.

function map(array, transform) {
    let mapped = [];
    for (let element of array) {
        mapped.push(transform(element));
    }
    return mapped;
}
let rtlScripts = SCRIPTS.filter(s => s.direction == "rtl");
console.log(map(rtlScripts, s => s.name));
// → ["Adlam", "Arabic", "Imperial Aramaic", ...]
Enter fullscreen mode Exit fullscreen mode
  • The reduce method is another function that takes in the input, and a condition based on which it combines elements. You can easily understand how reduce works based on the given example.
console.log([1, 2, 3, 4].reduce((a, b) => a + b));
// → 10
Enter fullscreen mode Exit fullscreen mode

Top comments (0)