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

Problems With Reduce Function

I have been trying to create an example problem for testing out the reduce function in javascript. the program it is intended to a single object from a set of input lines. function generateInputs(){ var inputLines = Math.floor(Math.random() *…
Leviathan_the_Great
  • 429
  • 1
  • 5
  • 14
1
vote
4 answers

Sum array of object of a key is returning NaN for more than 2 items in JavaScript

Following is my array - const products = [{ id: "1", quantity: 3 }, { id: "2", quantity: 3 }] console.log(products.reduce((acc, product) => acc.quantity + product.quantity)) // 6 -> Correct But if array contains more than 2 items it is…
Nesh
  • 2,389
  • 7
  • 33
  • 54
1
vote
1 answer

Await async functions to be done in reduce() function

I have a fuction which includes the following: const newThreads = newItems.reduce( (acc, item) => { request(item.href, function(error2, response2, html2){ if(!error2) { const thread = cheerio.load(html2) const today =…
Getter Jetter
  • 2,033
  • 1
  • 16
  • 37
1
vote
3 answers

JS (ES6): Reduce array based on object attribute

I have an array like this: const fruits = [ { fruit: 'apple', year: 2018 }, { fruit: 'apple', year: 2018 }, { fruit: 'banana', year: 2018 }, { fruit: 'orange', year: 2018 }, { fruit: 'apple', year: 2017 }, { fruit:…
Nibor
  • 679
  • 1
  • 8
  • 18
1
vote
1 answer

BackHandler.exitApp() is not closing the services of react-native android app

When I am using BackHandler.exitApp() to close the app and not removing the app from task manager window of the android, when I am again opening the app, it seems before it starts the service init, it start to call service functions. So as a…
Ankur Sardar
  • 405
  • 1
  • 6
  • 12
1
vote
4 answers

Compose - functional programming

I wanted to create my own implementation of compose This works const compose = (...fns) => fns.reduce((f,g) => (...args) => f(g(...args))); This not const compose1 = (...fns) => (...args) => fns.reduce((f,g) => f(g(...args))); . const multi = a…
Luke
  • 417
  • 1
  • 7
  • 18
1
vote
1 answer

How do I set multiple path values in jq at once?

I have figured out how to set a value in my JSON file, package.json, using setpath. Can I do this using a pattern? cat package.json | jq 'setpath(["dependencies", "acme-a"]; "mytagname")' What I would like to do is use a pattern like the following…
Stewart
  • 1,659
  • 4
  • 23
  • 35
1
vote
1 answer

Get the existing intersections between multiple object arrays

I have a couple of arrays which have some same ids. What I want to achieve is get the intersections between the arrays in plain javascript, no libraries. If there is a match of Ids and arraypicklist values are not equal between 2 or more arrays I…
Thomas
  • 181
  • 1
  • 9
1
vote
5 answers

JavaScript native groupBy reduce

I am using JavaScript native reduce, however I want to slightly change in the grouping to get my desired result. I have an array as follows: const people = [ {name: "John", age: 23, city: "Seattle", state: "WA"}, {name: "Mark", age: 25, city:…
Joe Saad
  • 1,940
  • 3
  • 22
  • 32
1
vote
6 answers

Javascript reduce not working after function?

Not too sure where I've gone wrong here, expecting to factorialize 5 (1*2*3*4*5 = 120) by turning 5 into a string of [1,2,3,4,5] and then using reduce to multiply the string all together. When I run the code, it just gives me [1,2,3,4,5]... var arr…
daggett
  • 239
  • 2
  • 4
  • 15
1
vote
3 answers

How to reduce if-condition looping - Swift

I know it sounds crazy, but just curious how I can reduce the if loop iteration for following? I have tried using guard let but stucked at some place. { if arenaEventItems == nil || arenaEventItems.count <= 0 { return } …
1
vote
0 answers

Reduction of a JavaScript object with numeric keys and float values causes unpredictable behaviour on iOS

Consider the following two objects: const ReductionMap = { '10': 10.5, '1000': 20.5, } const initialValues = { '10': 110, '1000': 120, } Using lodash v4.17.5 (here _), we reduce initialValues to a new object where each values has been…
Alexander Wallin
  • 1,394
  • 10
  • 22
1
vote
1 answer

How to calculate total using Javascript/ TypeScript reduce

I have the following array: [Title1: 111, Title2: 222, Title3: 333] This array is generated from a Web Socket Service and I want to accumulate the values using reduce. I have the following code, but I can't get it to work: this.liveDataPriceTotal =…
user3004118
  • 125
  • 3
  • 13
1
vote
4 answers

Showing maximum value of an object in an array when it as two max values

I have an array from the api response and its objects.I need to find the value which has maximum total and I have display it I'm unable to solve it , when there are two max values of the total.Is there any solution? myArray[ {time: "2018-12-02",…
Hema Nandagopal
  • 668
  • 12
  • 35
1
vote
3 answers

Using reduce on a list of dictionaries of dictionaries

Here is the given list. Pets = [{'f1': {'dogs': 2, 'cats': 3, 'fish': 1}, 'f2': {'dogs': 3, 'cats': 2}}, {'f1': {'dogs': 5, 'cats': 2, 'fish': 3}}] I need to use the map and reduce function so that I can have a final result of…
1 2 3
99
100