Reduce refers to the operation, where a array of items is reduced to a single value. Use with language tag like [c++], [google-sheets-formula]. Do NOT use alone.
Questions tagged [reduce]
3548 questions
1
vote
1 answer
Sum js file args in Node using reduce
I'm trying to get arguments ( fom second arg ) when running a js file in node then sum them.
var args = 0;
process.argv.reduce((a, b, c) => {
if (c > 1) {
console.log(+a + +b);
this.args += +a + +b;
}
return a
},…

infodev
- 4,673
- 17
- 65
- 138
1
vote
0 answers
Enumerable Functions Specifically the `.reduce()` function in JavaScript
I have this code here that I've set up:
var numbers = [1,2,3,4,5,6,7,8,9,2,3,4,5,6,1,2,3,4,5,6,7,8,8,4,3,2];
var greaterThan3 = numbers.filter (item => item >= 3);
console.log(greaterThan3);
greaterThan3.reduce((item, amount) => item + amount);
but…

Emily Butterfield
- 11
- 3
1
vote
1 answer
Python reduce function
I'm just learning Python, and do not understand the behavior I am getting from my reduce function. I have seen many examples where you can use reduce to perform an equivalent function to sum when you want to multiply:
f = [2,3,4]
reduce(lambda x,y:…

Sean Mahoney
- 131
- 7
1
vote
2 answers
Trying to reduce an array of objects to a similar but summed up array of objects
So I have an array of objects that looks like:
dataArray = [
{Revenue: 5, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
{Revenue: 10, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
{Revenue: 5, Date:…

awesome_treehouse
- 67
- 1
- 1
- 6
1
vote
0 answers
Merging column with reduce, getting key error
I have two csv files. I was trying to merge them.
The fist csv file 'Test_base1' looks like:
OBJECTID,STATEFP,COUNTYFP,TRACTCE,GEOID
1,12,105,10300,15000US121050103002
2,12,103,24804,15000US121030248041
3,12,105,10800,15000US121050108001
The…

Tashaho
- 163
- 1
- 2
- 11
1
vote
2 answers
How to use reduce on object
I'm trying to loop a object using reduce to generate a html list
const dropdownTemplate = (data) => {
console.log(data); // always empty
return `

Toni Michel Caubet
- 19,333
- 56
- 202
- 378
1
vote
1 answer
couchdb erlang reduce - aggregate object
Say I have a map that emits the following objects
{"basePoints": 2000, "bonusPoints": 1000}
{"basePoints": 1000, "bonusPoints": 50}
{"basePoints": 10000, "bonusPoints": 5000}
How could I write a reduce in Erlang (not javascript) that would return…

user1149843
- 31
- 2
1
vote
1 answer
Swap key and value and accumulate values
I have the following JSON snippet:
{
"a": [ 1, "a:111" ],
"b": [ 2, "a:111", "irrelevant" ],
"c": [ 1, "a:222" ],
"d": [ 1, "b:222" ],
"e": [ 2, "b:222", "irrelevant"]
}
and I would like to swap the key with the second value of the array…
user118861
1
vote
3 answers
reduce a list in scala by value
How can I reduce a list like below concisely
Seq[Temp] = List(Temp(a,1), Temp(a,2), Temp(b,1))
to
List(Temp(a,2), Temp(b,1))
Only keep Temp objects with unique first param and max of second param.
My solution is with lot of groupBys and reduces…

yalkris
- 2,596
- 5
- 31
- 51
1
vote
1 answer
Execute arithmetic operation on concat String with Java Streams
Hello guys and thank you all for helping.
I have a Stream where a String can be something like "1+5*2-4" //= 8 (its ok if I calculate it from left to right)
This operation is no problem, but i try to do it now with Streams only.It means I…

Tristate
- 1,498
- 2
- 18
- 38
1
vote
3 answers
Creating an array of custom objects from nested data structure
I'm working on a project that requires me to massage some API data (shown in the snippet below as 'apiData'). The data structure I ultimately need for the charting library I'm using (Recharts) is this:
[
{ date: '2018-04-24', TSLA: 283.37, AAPL:…

Adam Bohannon
- 108
- 1
- 2
- 8
1
vote
1 answer
javascript least amount of elements from an integer array that can be used to get to a total value
please can somebody help?
If i have a total or a sum for instance 91
How can I create an array of the least amount of elements needed to get to the total value?
[50, 20, 10 , 5, 3, 2, 1] totaling this array will provide 91.
I know how to perform…

Iwan Ross
- 196
- 2
- 10
1
vote
3 answers
Recursive .reduce() to output an array of parents
I have an flat array of Folders like this one :
const foldersArray = [{id: "1", parentId: null, name: "folder1"}, {id: "2", parentId: null, name: "folder2"}, {id: "1.1", parentId: 1, name: "folder1.1"}, {id: "1.1.1", parentId: "1.1", name:…

Pierre_T
- 1,094
- 12
- 29
1
vote
0 answers
How to sum together rows of numpy 2d array according to partition
I am wondering what is the fastest way, given a shape (n, m) numpy array and a shape (p) numpy array describing a partition of the range 0 to n (for example, one such partition for n=6 would be: [0, 2, 4], meaning the indices are partitioned as (0,…

ffffffyyyy
- 117
- 2
- 7
1
vote
2 answers
foldr that doesn't crash and works reasonably well
I want a foldr that's similar to the foldr in Haskell or lisp. My foldr causes stack overflow on large arrays, and probably because large numbers of pending operations on the stack can't be reduced until it hits the base case. How would you optimize…

user12457
- 191
- 4