Questions tagged [array-reduce]

An array reduce function allows you to iteratively reduce an array to a single value using a callback function. PHP: array_reduce(), JavaScript: Array.prototype.reduce()

An array reduce function allows you to implement a algorithm by using a callback function to iteratively reduce an array to a single value.

Documentation

Example

Consider this simple code, which will result in calculating a total value of 15 by calling the sum() function 5 times.

PHP:

function sum($total, $item) {
    return $total + $item;
}

$data = array(1, 2, 3, 4, 5);

$result = array_reduce($data, 'sum', 0);

JavaScript:

const sum = (total, item) => total + item;

const data = [1, 2, 3, 4, 5];

const result = data.reduce(sum, 0);

The sum() function will be called 5 times, once for each element of the array:

  • The first time, it uses the starting value of 0 that we provided, and processes the first item:

      sum(0, 1) // returns 1
    
  • The second time, it uses the return value from the first call, and processes the second item:

      sum(1, 2) // returns 3
    
  • Each time after that, it uses the previous return value, and processes the next item in the array:

      sum(3, 3) // return 6
      sum(6, 4) // returns 10
      sum(10, 5) // returns 15
    
  • And finally, the value returned from the last call is returned back as the result of the array_reduce() function:

      $result = 15;
    
58 questions
2
votes
1 answer

Javascript, uploading several files within an Array.reduce with Promises, how?

Evolving from Javascript, spliced FileReader for large files with Promises, how?, which showed me how a Promise can also resolve a function, now I am stuck with the same but inside an Array.reduce function. The goal is that I want to upload a file…
GWorking
  • 4,011
  • 10
  • 49
  • 90
1
vote
0 answers

how to pass a value into a next iteration of array_reduce in immutable style?

I've got a two-dimensional array: $array = [ ['property', 'group1', 'was', 'added'], ['property', 'ouch', 'was', 'removed'], ['property', 'ouch', 'was', 'updated'], ['property',…
1
vote
1 answer

How to get the wanted result using reduce javascript?

I'm trying to understand how the reduce method work. The groupBy function only return object that is grouped by category. How do I get the wanted result below? I have tried many ways but none works. const PRODUCTS = [ { category: "Fruits",…
DanNg
  • 45
  • 3
1
vote
1 answer

Count bottom-level elements in nested object of objects of arrays

I have an "object of objects of arrays" and I simply need to get the total count of elements in the "bottom level" arrays ...so with the example below, I need to return 19 (counting elements a to s): var arr={ 'AA': { 'aa': ['a','b'], …
ashleedawg
  • 20,365
  • 9
  • 72
  • 105
1
vote
1 answer

How to group array of dates by start time and end time and subtract last value - first value?

I have array of dates grouped by day and I want to group dates by time start and end and subtract last value - first value. The time start and end are also in an array. Data array: const data = [ { x: "2021-10-17T24:00:00.000Z", y: 52 }, { x:…
Dev M
  • 1,519
  • 3
  • 29
  • 50
1
vote
2 answers

How to group array of dates by year and subtract last value - first value?

I have the array of dates and I want to group dates by year and subtract the last value - the first value. for example, I have the following array: const data = [ { x: "2021-10-17T14:38:45.540Z", y: 2 }, { x: "2021-10-17T13:38:45.540Z", y: 2 }, …
Dev M
  • 1,519
  • 3
  • 29
  • 50
1
vote
1 answer

Reduce not work correctly i need add exeption / exlusion

i have an array like that in $_POST var; but need know that in some cases the array is a hugge multilevel from json: array ( 'idprocess' => 'f-gen-dato1', 'idform' => 'f-gen-dato2', ) OR: array ( array ( 'idprocess1' => 'f-gen-dato1', …
user10771503
1
vote
1 answer

Understanding a reduce function with if and bracket notation

Please explain the code and elaborate what is running behind the code. I'm confused with if part if(! acc[key]). does it mean if key not in acc and set key with value array and jump out of if statement and push the obj in acc key value? In case if…
daneillone
  • 95
  • 1
  • 1
  • 8
1
vote
3 answers

Push value to object if key already exists during Array.map

I have an array that looks like this: let movies = [ 'terminator.1', 'terminator.2', 'terminator.3', 'harry-potter.1', 'harry-potter.3', 'harry-potter.2', 'star-wars.1' ] and I would like to have an object like this: { "terminator":…
OmarAguinaga
  • 707
  • 1
  • 8
  • 17
1
vote
3 answers

How would I return minimum value in Array of Std Class Objects (PHP)?

I have an array of StdClass Objects and want to return the "partner_code" with the minimum value for key "price". So for this example I would like to return partner_code "AC" as it is the partner with the lowest price. I tried using array_reduce,…
1
vote
1 answer

Array reduce with associative array

I have an array like this $filter_array = Array ( [0] => Array ( [fv_id] => 1 [fv_value] => Red [filter_id] => 1 [filter_name] => Color ) …
phpnerd
  • 850
  • 1
  • 10
  • 25
1
vote
1 answer

Use array_reduce and array_map for dynamic variables

I want to have that variable $total[$i] giving the result of the following function: $total[$i] = (array_reduce((array_map(function($x, $y) { return $x * $y; }, $corp_resp[$i], $corp_resp_template)),function($carry,$item){return…
1
vote
2 answers

How to combine associative arrays with different keys?

I am trying to merge 5 associative arrays which having different keys. This is the result after using array merge on 5 arrays array_merge($bucketExlData1, $bucketExlData2, $bucketExlData3, $bucketExlData4, $bucketExlData5) result array:5 [▼ 0 =>…
1
vote
1 answer

array_reduce to use dynamic variables pass in second function

I have below $test array Array ( [0] => Array ( [quantity] => 3 [stock_id] => _PHONE ) [1] => Array ( [quantity] => 3 [stock_id] => 102 ) [2] =>…
SonDang
  • 1,468
  • 1
  • 15
  • 21
1
vote
1 answer