Questions tagged [reduce]

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.

Documentation in respective languages:

3548 questions
1
vote
1 answer

Promise not resolving before returning

I am having a hard time figuring out how promises work. I have the following code that I am executing iterate through an array. runOpts.automationResults.reduce(function (p, val) { return p.then(function () { return …
1
vote
3 answers

Using reduce to get a sum of object values, but the answer is a few decimals short (JS)

I have the following code: function checkCashRegister(price, cash, cid) { // Move cid into an object var cashReg = cid.reduce(function(prev, curr) { prev[curr[0]] = curr[1]; return prev; }, {}); // Total money…
1
vote
1 answer

Create a new array of objects building on previous values

I'm trying to build an array of objects out of another array of objects: const items = [ { id: 'thing-1', size: { height: 50 }, }, { id: 'thing-2', size: { height: 100 } }, { …
Matt Coady
  • 3,418
  • 5
  • 38
  • 63
1
vote
2 answers

JS: Counting Elements in Array -> Returning Array of Arrays

I have an array which looks like this: fruits = ['Apple', 'Apple', 'Peach', 'Apple', 'Banana', 'Pear', 'Apple', 'Banana', 'Peach']; Now I'm trying to count the containing elements to get a result which looks like this: [ ['Apple', 4], ['Peach',…
Nibor
  • 679
  • 1
  • 8
  • 18
1
vote
4 answers

Reduce array with javascript

I have an array like this : myArray = [40, 40, 40, 40, 40, 80, 40, 40, 40, 40, 40, 40, 40, 40, 80, 40, 40, 40, 40, 40, 40, 40, 40] I would like to reduce it like this : myNewArray =[40, 80, 40, 80,40]
Manheman
  • 39
  • 1
1
vote
3 answers

Javascript reduce function error

I am trying to play with the Reduce function in JavaScript. What I am trying to achieve can be easily done via the filter function but I am trying to do it via Reduce. I am trying to retrieve all the people where the age is greater or equal to 18…
Manish
  • 21
  • 5
1
vote
1 answer

How to reduce a compact buffer in scala?

Here is my RDD: scala> grouped_final_resultMap.first res20: (String, Iterable[Float]) = (2014-02-01,CompactBuffer(239.96, 129.99, 49.98, 100.0, 399.98)) What I want to do is the sum up all the items in the Iterable[Float] in that RDD’s _2-nd…
Choix
  • 555
  • 1
  • 12
  • 28
1
vote
3 answers

discrepancy in array.reduce javascript

I spent hours trying to debug a particular code, at the very end i noticed something i don't understand with arr reduce function. Take the below code example var arr = [{ start: 1, value: 4 }, { start: 2, value: 5 }, { start: 3, …
Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116
1
vote
2 answers

Use reduce or map to upper-case every property in a JavaScript object

Is there a way to upper-case all keys of an object using map or reduce so the result may be returned directly? It works fine with forEach var o = { fname: 'john', lname: 'doe' } var result = {} Object.entries(o) .forEach((el) => { …
1252748
  • 14,597
  • 32
  • 109
  • 229
1
vote
4 answers

Concise way to reduce where one of the states throws an exception

I have a bunch of these: Validation a; Validation b; Validation c; Here are some of their methods: boolean isValid(); boolean isInvalid(); // === !isValid() String getError(); Now, I was trying to do…
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
1
vote
2 answers

Behaviour of Scala ReduceLeft

In the following code snippet I'm using reduceLeft and foreach loop to find the sum of the differences of a number against all of the list members. I was expecting that the result from these two would be the same (1050) but reduceLeft is adding…
RAQ
  • 119
  • 8
1
vote
1 answer

How does cleanup() method work?

I am currently new to Hadoop. So I have this solved piece of code in MapReduce which finds out the "parts of a country with most 'Data Engineer' jobs for each year" (for example, if the data of the format (Year,Region,Count(Jobs)) is "2016,'XYZ',35"…
Anand Raina
  • 17
  • 1
  • 8
1
vote
1 answer

Sorting array of object inside reduce in JavaScript

I am trying to do sort inside the reduce and I thought I have everything correct but still my result is not sorted as desired. Here is the code snippet I have: var studentInfo = [ { studentId: 1, addresses: [ {street: '123 Main…
αƞjiβ
  • 3,056
  • 14
  • 58
  • 95
1
vote
1 answer

transform_reduce with max_element

I am trying to use transform_reduce to transform a set of numbers and then reduce it to the maximum of those elements. auto lambda = [](uint64_t x) { return function(x); }; counting_iterator start(1); counting_iterator
Andrew O'Rourke
  • 127
  • 1
  • 9
1
vote
2 answers

Reducing the cyclomatic complexity, multiple if statements java

I have a lot more of these else-if conditions that I have not included. How could I refactor this to reduce the cyclomatic complexity? if (ONE.equalsIgnoreCase(eachTag.getNodeName())) { myclassDto.setOne(formatter …
user3431624
  • 171
  • 1
  • 1
  • 10
1 2 3
99
100