Lauren Parker
Lauren Parker

Reputation: 33

Nodejs how perform reduce() on array

I need to perform a reducer function on array.

My reducer function is let rdx = function(a,b){return a*b}

What is the best method in nodejs?

Upvotes: 2

Views: 1113

Answers (2)

Here you a have an example of how to use reduce function in nodejs

const array = [1,2,3,4,5];
const result = array.reduce((before, value, index) => {
  if (value < 3) {
    before.push(value);
  }
  return before;
}, []);
console.log(result);

or you can use lodash if you prefer and is like the same.

Upvotes: 2

SteveWoods
SteveWoods

Reputation: 16

Define an array called yourarray for example and write

var result = yourarray.reduce(rdx);
console.log(result);

Then execute and check on your console the result

Upvotes: 0

Related Questions