1

I am reading a book about Angular but I can't find any documentation about this brackets usage for the lambda expression [hours, rate]) => this.total = hours * rate. Although I understand that I can use these parameters (hours and rate) from inside the body of the lambda expression. I don't understand why this (hours, rate) => this.total = hours * rate) doesn't work and why do I have to use the brackets [].

Observable.combineLatest(this.invoiceForm.get('hours').valueChanges,
this.invoiceForm.get('rate').valueChanges).subscribe(
  ([hours, rate]) => this.total = hours * rate);

Can somebody explain me what it means and where I can find documentation about that usage.

Note: I know what combineLatest does what I don't understand is the lambda expression usage with those brackets.

Dexygen
  • 12,287
  • 13
  • 80
  • 147
Alfredo Osorio
  • 11,297
  • 12
  • 56
  • 84
  • It means that the parameter is an array with 2 items and the items will be "destructured" (see @t.niese answer) as follows - 1st item to variable `hours`, 2nd to variable `rate`. If the array is longer, then `rate` will contain the array without the 1st item. – Karel Frajták Jan 22 '19 at 07:36

2 Answers2

6

It is like the destructuring assignment:

function test([a, b]) {
  console.log(a, ',', b)

}

test([1, 2])

The [a, b] is the one parameter of your function. And if an array is passed as first argument to the function, then the first element of the array will be stored in a and the second one in b.

t.niese
  • 39,256
  • 9
  • 74
  • 101
0

combineLatest rx operator:

When any observable emits a value, emit the latest value from each.

https://www.learnrxjs.io/operators/combination/combinelatest.html

Sanid Sa
  • 264
  • 3
  • 9
  • I know what combineLatest means what I don't understand is the brackets for the lambda expression. – Alfredo Osorio Jan 22 '19 at 07:25
  • Brackets mean Array, we are accepting values that combineLatest emits as valuepairs represented as array. In case of more params combineLatest(a,b,c) , you would have 3 number of params as emitted value inside of those brackets. – Sanid Sa Jan 22 '19 at 07:28