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
0 answers

Summing the values based on Key of RDD

I have data set of crimes happened from 2001 up till now. I want to calculate no_of_crimes happened per year. My code which i have tried is val inp =…
Niketa
  • 453
  • 2
  • 9
  • 24
1
vote
2 answers

Javascript reduce - split accumulator in multiple variables

Why t becomes undefined in second iteration? function findShortest(s){ const arr = s.split(' '); arr.reduce(([acc, t], curr, idx) => { console.log('t', t) if (curr.length < t.length) { // some code t = curr; }…
gizmo
  • 191
  • 1
  • 11
1
vote
2 answers

Why does javascript reduce function output an array of strings

I am trying to sum up the numbers inside an array e.g. from here: https://jsfiddle.net/4r8dtxhz/12/ here is the Code: var someObj = [{name:"hi", series: [1,2,10,4,5,6]},{name:"ho",series:[3,7,6,9,12,1,3,20,3,1]}] for (var doc of someObj)…
MMM
  • 287
  • 1
  • 5
  • 17
1
vote
1 answer

reduce() method is returning NaN from an array of objects

I want to obtain the sum of total of votes using reduce(), but I am always receiving NaN as a result, instead of 20. I have the following js code: const choiseArray=[ {choice: "Shooter",url:"/questions/10/choices/01",votes:3}, {choice:…
1
vote
2 answers

Discrepancy between python sum(list) and reduce(lambda x, y : x+y, list)?

Whats the difference between sum(list) and reduce(lambda total , element : total + element, list) orderItemsMap = [149.94, 250.0, 199.99, 249.9, 149.94] >>> sum(orderItemsMap) 999.77 orderItemsRevenue = reduce(lambda total , element : total…
1
vote
3 answers

How to sum property in array of objects, based on a condition

I'm a junior Web Developer, looking for some guidance solving a problem. Please excuse me if I'm missing anything integral, as this my first time posting here. I have an array of some data returned like so: [ {x: Date(1234), y: 0} {x: Date(1235),…
momentum
  • 13
  • 3
1
vote
4 answers

JavaScript for loop stops running in the middle

I'm trying to return an array of numbers in descending order (biggest in the front). My function seems to work but just quits in the middle. let array = [1, 9, 8, 7, 2, 6, 3, 5]; let sorted = []; function sortNumbers(array) { for (var i =…
brandongatlin
  • 17
  • 1
  • 5
1
vote
3 answers

javascript reduce over multiple keys of objects in an array

I have have map of Object of the following type (example data below) {[date:string]:Array<{[type:string]:{amount:number, price:number}}>} And I need to reduce the array to a single object of amount sum and price averaged I have read a couple of…
Han Che
  • 8,239
  • 19
  • 70
  • 116
1
vote
2 answers

Reduce a list in a specific way

I have a list of strings which looks like this: ['(num1, num2):1', '(num3, num4):1', '(num5, num6):1', '(num7, num8):1'] What I try to achieve is to reduce this list and combine every two elements and I want to do this until there is only one big…
Tobias
  • 564
  • 3
  • 13
1
vote
1 answer

Why can't I reduce(+) on a seq of integers in Scala?

I want to get the sum of a seq of Integers in Scala. In my mind I want to fold a plus over the integers like this: val seqOfIntegers:Seq[Int] = Seq(1, 2, 3, 4, 5) val sumOfIntegers = seqOfIntegers.reduce(+) This isn't valid. Instead I have to…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
1
vote
2 answers

How to compare time property in each object group in a array of objects

I do have a array of objects, which each group of objects need to filtered based on thier time property, and in output it should only return ithems which have less then 1 minute time difference and remove those which have more then 1 minute time…
Emad Dehnavi
  • 3,262
  • 4
  • 19
  • 44
1
vote
2 answers

Convert pickBy and mapValues from lodash to function

I'm trying to convert this function that uses pickBy and mapValues from lodash to a function that doesn't use lodash. import mapValues from 'lodash/mapValues'; import pickBy from 'lodash/pickBy'; function clinicsWithInvitations(upcomingClinics)…
Lizz Parody
  • 1,705
  • 11
  • 29
  • 48
1
vote
5 answers

purrr::reduce/reduce2 or mapped mutate_at()? - functions applied to respective column

I have a map of functions I want to apply to their respective columns. Is there something liked a mapped mutate_at? my_map <- data_frame(col = names(iris)[-5], calc = rep(c("floor", "ceiling"), 2)) my_map # A tibble: 4 x 2 col …
1
vote
3 answers

How To Group Object Using reduce Function javascript

I have been stuck on this for 8 days now. Let's say here is my response object array: var items = [ { name: 'dell-66', price: 200, id: 12, }, { name: 'hp-44', price: 100, id: 10, }, { name: 'acer-33', price: 250, …
1
vote
2 answers

Getting the Sum of a Particular Field for the Objects in an Array in JavaScript

I know I can use reduce to sum the numbers of an array, like so: let numArray = [1, 2, 3, 4].reduce((a, b) => a + b, 0); console.log(numArray); However, if I have an array of objects, of which one field's value is a number, how would I go about…
Muirik
  • 6,049
  • 7
  • 58
  • 116