Javascript - Functional Programming

About

Code - Functional programming (FP) - Collection Operations in javascript.

All functional programming functions are array operations - See Doc

Example

To get the cookies

console.log("Functional Programming: The cookies that we see with Javascript");

document.cookie
    .split(";")
    .map(cookie => cookie.split("="))
    .forEach(cookie=>console.log(cookie));

List

Collector

All function returns an array.

A few returns an element such as:

  • find: returns the value of the first element in the provided array that satisfies the provided testing function.
  • findIndex() method returns the index of the first element in the array that satisfies the provided testing function.

map

Javascript - Map function (Functional programming)

Map example 1

data = [
  { name: 'Nicolas', firstname: 'Gerard', surname: 'nico' },
  { name: 'Rixt', firstname: 'Van Der Veen', surname: 'konigin' }
]

console.log("Name");
names = data.map( function (d) { return d.name } )
console.log(names);

console.log("FirstName");
firstnames = data.map( function (d) { return d.firstname } )
console.log(firstnames);

console.log("Surname");
surnames = data.map( function (d) { return d.surname } )
console.log(surnames);

Map example 2 - select and process

var myList = [].slice.call(document.querySelectorAll('selector'))
myList.map(function (element) {
  // ....
});

Filter

filter functional programming function.

Example a split on a string that does contains a separator without value create an empty string element. We filter it below.

let s = "aStringWithoutSeparator.";

console.log("With filtering");
console.log( s.split ('.').filter(el => {return el.length != 0}));

console.log("Without filtering");
console.log(s.split ('.'))

Terminal

terminal function are all array function that does not produce an array such as:





Discover More
Cookie Devtool
How to manage Cookies in the Browser via Javascript?

This article is HTTP cookies management in the client side (browser) via javascript. Cookie are one way to store data in the browser. document.cookie is a property of the browser document that returns...
Javascript - Array

array in Javascript can contain any type of data. Different types of values can be stored within the same array Because arrays are special objects (as typeof implies), they can also have properties, including...
Javascript - Integer

Integer aren't implemented in javascript, only double exists. See With integers, you have to take care that all calculations fit within the range between –2^53 and 2^53, but you don’t have to worry...
Javascript - Map function (Functional programming)

The functional programming function map in a Javascript context is only an array method as all functional programming javascript method. map With an Arrow function as argument Letters that we...
Javascript - Set

The Set interface represents a set data type and works only with primitive type as there is no way to define an object equality function. object Jsdoc Declaration Add/Delete Size Contains...
Javascript - foreach instruction

How to loop with foreach in Javascript



Share this page:
Follow us:
Task Runner