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
0
votes
1 answer

Javascript - refactor array - array reduce

What is the best way to refactor(ise) an array / array of objects ? I was thinking of using array.reduce, but could not figure out a solution. for example, transform this array : const carsInput = [ { brand: 'volkswagen', …
midnight
  • 25
  • 5
0
votes
2 answers

how to shrink an array if two consecutive numbers in an array are equal then remove one and increment other

How to shrink an array if two consecutive numbers in an array are equal then remove one and increment other Example 1: int a[6]={2,2,3,4,4,4}; // Output: 6 Example 2: int b[7]={1,2,2,2,4,2,4}; // Output: {1,3,2,4,2,4}
0
votes
2 answers

JS: How to combine Object from two different arrays into one Array/Object

If have two arrays (content & ids) which i need to combine in one array (combined). First Array: let content = [ { "Name": "Max Mustermann" }, { "Name": "Berta Beispiel" }, { "Name": "Zacharias Zufall" } ] Second Array: let ids = [ …
Robin
  • 111
  • 6
0
votes
1 answer

Display array result in html table using array_walk or array_map

I am working with users data inside array and I want to print user data inside html table without using foreach loop but alternative of that using array_walk() db->get('user')->result(); echo…
Anonymous
  • 1,074
  • 3
  • 13
  • 37
0
votes
4 answers

Reduce an array to change content

How can I use array.reduce to change the way the content of the array is. I don't want to do any math on the content. Orinal array: var myArray = [ {id:1, name:'name01', value:11}, {id:2, name:'name02', value:22}, {id:3, name:'name03',…
Somename
  • 3,376
  • 16
  • 42
  • 84
0
votes
5 answers

Group multiple elements in array with JavaScript

I have an array [ { price: 10 }, { price: 10 }, { price: 10 }, { price: 10 }, { price: 20 }, { price: 20 }, ] and I want it transformed into [ { numElements: 4, price: 10 }, { numElements: 2, price: 20 }, ] I have tried using…
Jamgreen
  • 10,329
  • 29
  • 113
  • 224
0
votes
1 answer

Fatal error: Unsupported operand types in PHP

I have that code where i want to multiply corp_resp for corp_resp_template and sum dynamically. $total = (array_reduce((array_map(function($x, $y) { return $x * $y; }, $corp_resp,…
-1
votes
4 answers

How to properly map or reduce an Array of Arrays of objects in javascript?

I have the following array of array of objects that is a result of a CloudWatch query using AWS-SDK-V3 JS: const respCW = [ [ { field: '@timestamp', value: '2022-10-25 15:30:36.685' }, { field: '@message', value: 'foo' }, …
IgorAlves
  • 5,086
  • 10
  • 52
  • 83
-1
votes
3 answers

Create a new array with key as value of old array with total count?

I have a array with following values.I am trying to create a new array using array php array functions and trying to max avoid foreach. The key we are using for new array is "status" and depending on status we make new array for each mail…
Mail4Coder
  • 23
  • 6
-1
votes
3 answers

find the minimum and maximum value in array group php

Array ( [0] => Array ( [package] => LTE_15AGB [value] => Array ( [0] => 52690 [1] => 24700 [2] => 43972 [3] =>…
Arebhy Sridaran
  • 586
  • 12
  • 28
-2
votes
1 answer

Using array.reduce to count matches in an array

I am trying to replicate the function below using Array.reduce private getCount = (str, value) => { var count = 0; const everything = ['Key1', 'Key2', 'Key3', 'Key4', 'Key5']; for (let i = 0; i < everything.length; i++) { if (everything === value)…
LearnToImprove
  • 345
  • 5
  • 15
-3
votes
1 answer

Need someone to help me understand these two solutions - map, reduce, match, regex

Description: I've been playing around with Codingames today, and came across this problem in a Clash of Code. Problem: Given a word, output the sum of the word's index according to it's place in the ABC. Example: word = "HI" result =…
Beca_1923
  • 19
  • 3
-5
votes
1 answer

JavaScript code to fetch range of dates from array of objects depending on one attribute of the array using array.reduce()

var developers = [ { name: "Joe", age: 23, overallLevel: "high", date: "Aug 14, 2015" }, { name: "Sue", age: 28, overallLevel: "advanced", date: "Aug 11, 2015" }, { name: "Jon", age: 32, overallLevel: "high", date: "Aug 10, 2015" }, { name: "Bob",…
Saily Jadhav
  • 149
  • 2
  • 10
1 2 3
4