Tech Talk 03: JavaScript Map vs. Filter vs. Reduce
Filter vs Map vs Reduce Credit: Rafsanjani

Tech Talk 03: JavaScript Map vs. Filter vs. Reduce

So without further due, lets get started.

Map

A map is an array function that operates on an array. We can pass a custom function inside it to perform some operation on an array.

Example:

Suppose, we have an array of numbers, we want to double each value. using map, it can be done very easily. There are two varaions, short hand and callback function approach.

const numbers = [1, 2, 3, 4];
const doubled = numbers.map(value => value * 2);
Output: [2, 4, 6, 8]        
function doubleIt(n){
   return n * 2;
}

const numbers = [1, 2, 3, 4];
const doubled = numbers.map(doubleIt);
Output: [2, 4, 6, 8]        


Filter

Similar to map, filter also performs on array but in a different approach. Filter applies condition on each element of the input array, if the condition is full filled, only then it is pushed to the output array otherwise discarded.

Example:

Suppose, from an array of numbers, we wan to sort out the even ones.

const numbers = [1, 2, 3, 4];
const evens = numbers.filter(value => value % 2 === 0);
Output: [2, 4]        


Reduce

This function takes an array and transform it to a single value. It is useful when we are trying to achieve some sort of transformed value out of an array.

Example:

Suppose, we want to get the total sum from an array of number.

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce(function (result, value) {
  return result + value;
});

Output: 10        

That's all folks!

要查看或添加评论,请登录

Sadat Rafsanjani的更多文章

  • Software Firm? Product-based or Service-based

    Software Firm? Product-based or Service-based

    So, without further due, lets get started. What it is? A software firm creates or innovates new ideas, products and…

  • Bloom Filter

    Bloom Filter

    What is it? A space efficient probabilistic (uses probability math) data structure. It is used to search element in a…

  • Linux File Permissions

    Linux File Permissions

    So without further due lets get started. What is it? File permission is a part of Linux administration where the system…

  • Tech Layoffs: You are Not Safe

    Tech Layoffs: You are Not Safe

    Tech layoffs are common these days. In fact experts are saying the chance of worsening is more likely to happen this…

  • Tech Talk 04: Integrating Google Translate in Angular

    Tech Talk 04: Integrating Google Translate in Angular

    So, without further due, lets get started. Introduction Google translate library is available to use for free.

  • Tech Talk 02: Two-Phase Commit (2PC)

    Tech Talk 02: Two-Phase Commit (2PC)

    So, without further due, lets get started! What is 2PC? Two-Phase Commit aka Tupac (2PC) is a protocol or distributed…

  • Tech Talk 01: JavaScript Event Loop

    Tech Talk 01: JavaScript Event Loop

    For those of us working in JavaScript for a while, we all know about the event loop mechanism. In this talk, we discuss…

  • 5 Minutes Software Engineering: Engineering Best Practices & Principle

    5 Minutes Software Engineering: Engineering Best Practices & Principle

    KISS (Keep It Simple, Stupid) Try to keep your code simple and small as much as possible. Instead of deep nesting, lots…

  • Architect of Horror!-Java Virtual Machine

    Architect of Horror!-Java Virtual Machine

    Without further due, lets get started! What is JVM? Java virtual Machine (JVM) is an abstract machine. What is an…

  • Houston, We have a Problem! SSL Certificate Demystified

    Houston, We have a Problem! SSL Certificate Demystified

    Problem: How to provide a safe and secure environment for website? Why visitors should trust it? The Solution: Secure…

社区洞察

其他会员也浏览了