JS reduce()

Man hat ein array und möchte zum Beispiel alle Zahlen im Array kumulieren. Mit der Javascript Array Function reduce() geht das ganz leicht.

const arr = [1,2,3,4,5,6];
let cum = 0;
arr.reduce((acc, curr, index) => cum += curr, 0);
console.log('cum',cum)

Leave a Comment

Your email address will not be published. Required fields are marked *

*

*

Empfholende Artikel


Alphanumeric sorting of an array with objects according to the value of an object

June 30, 2022

Especially in the frontend it happens quite often that you want to sort an array, an array with objects according to a certain pattern. Javascript has very performant and nice functions. But you can use these functions not only in the frontend. If you write your backend with NodeJS you will also appreciate the array […]

Return days of a month in an array

March 14, 2022

You need all days of a certain month then you can use this function: getDaysInMonth = (month,year) => new Date(year, month, 0).getDate();console.log( […Array(getDaysInMonth(3, 2022)).keys()] ); Greets!

JS flat()

March 9, 2022

Es kommt schon mal vor das man geschachtelte arrays bekommt.Zum Beispiel: Man möchte aus einem geschachteltem Array alle Werte in einem Array sammeln. Mit der array Funktion flat() ist das kein Problem. Auch mehrstuffig verschachtelte Arrays kann man geradeziehen (flatten). Indem man der flat Funktion die Anzahl der verschachtelungen die aufgelöst werden sollen, mitgibt. Da […]

JS bind()

March 8, 2022

Ein einfaches besipiel um JS bind() function zu verdeutlichen:

Javascript – Fakultät berechnen

January 19, 2022

Ab und an braucht man das und nicht nur in der Kombinatorik. Wer es etwas übersichtlicher braucht (und dazu zähle ich mich auch) kann es auch in einer for schleife machen.

Javascript – Was ist eigentlich Promisification?

January 6, 2022

“Promisification” ist ein lange Wort für eine ganz simple Sache. Es geht eigentlich um die immer gleiche Frage, sobald man das erste Mal in JS eine API Anfrage stellt. Wie krieg ich das Zeug in den globalen Scope? Man bedient sich ganz einfach damit, die asynchronität zu stoppen und mal ein päuschen einzulegen. Sobald dann […]