Niels
Niels

Reputation: 61

multiple dot methods underneath eachother

I would like to know something about a code fragment I saw.

In the video he has a variable. And then he puts dot methods underneath each other that keeps manipulating that same beginning variable consecutively. It has spacing in front of it and then chains into lower lines all with . notation in front of it.

The video where I saw it now is this one: https://www.youtube.com/watch?v=1DMolJ2FrNY&list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84&index=4

At exactly the 6 minutes mark he has 3 of those dot methods underneath each other. Where can I find something about that? Cause I don't want to blindly copy the behavior.

This is code from the video:

import fs from 'fs'

var output = fs.readFileSync('data.txt', 'utf8')
  .trim()
  .split('\n')
  .map(line => line.split('\t'))

console.log('output', output)

It is the dot usage here that I'm interested in.

Upvotes: 2

Views: 494

Answers (1)

Odinn
Odinn

Reputation: 1106

It's called chaining

Example:

 [1,2,34,5,6]
.map((item)=> item + 1)
.filter((item) => item > 3)
.toString()

Each time method return the array, and you can continue chaining with existing Array methods.

In your example he receives a String, then split it to the Array, and map it. The only thing you have to understand is what type you will receive after each method.

Upvotes: 3

Related Questions