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", ...}, ...]
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", ...]
- 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
Top comments (0)