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

having an issue with Array.reduce(). I have somewhat correct output. But I can't figure out the rest

Here are the instructions: Pretend that there is no Array.filter() method. Use Array.reduce() to filter out only the objects from the provided array with a price property of 10 or over. Pretend there there is no Array.filter() method, only…
aw_codes
  • 3
  • 1
0
votes
1 answer

How can I assign properties to an object in php specifically using array_reduce?

I have a simple php array that looks like this & I am looping over it to assign its keys as properties to an stdClass object & its values as the values of those keys. My code looks like this $arr = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4,…
Richard
  • 412
  • 6
  • 9
0
votes
0 answers

Javascript: how to arrange array in properly way?

I'm new to JavaScript. I want to arrange the following array { "movies": [ { "id": "1", "competition":1, "time":1670854636, "title": "Star Wars", "releaseYear": "1977" }, { "id": "2", "competition":1, "time":1670854636, "title": "Back to the…
Adriano Bellavita
  • 143
  • 1
  • 3
  • 12
0
votes
1 answer

how to use array_reduce in immutable style?

I have been going through online study on a resource I quite liked till this very moment. The problem is they want my perfectly working array_reduce function is written "in immutable style". Which means that nothing should mutate inside, even…
0
votes
1 answer

Laravel/PHP: Add another condition in array_reduce function

I have multidimensional array and I want to sum all the same value with a certain condition. Here's what my array looks like $input_taxes = [ [ "tax_account_id" => 65, "checkbox" => false, "amount" => "13950.89", ], …
Angel
  • 966
  • 4
  • 25
  • 60
0
votes
3 answers

How to restructure data using reduce?

I am getting data from API structured like this: interface ProductModel { prod_id: string, brand: string, ... } interface OrderModel { order_id: string, prod_id: string, ... } const data = { products: ProductModel[], …
0
votes
1 answer

TypeScript - Array reduce return object with specific type/interface

I'm creating a generic function that gets properties from an object using array.reduce. I would like the return type to only return the type of properties extracted from the original object type Sample = { name: string; age: number; isAlive:…
CRIS
  • 335
  • 3
  • 17
0
votes
0 answers

Reduce IteratorIterable to exact type T

I need to write an hook that fetch the URL query parameters to return an object of strictly type T export function useQueryParams(): [ T, ({ key, value }: { key: keyof T; value: string }) => void, ] { const [searchParams, setSearchParams] =…
0
votes
5 answers

Remove duplicate from array of objects based on value of properties in JavaScript

How can I remove duplicates from an array someArray like below based on the name property given the condition that if name is the same for two elements but for one them the type is new, the original one (without type new) will be retained? someArray…
Smarajit
  • 143
  • 4
  • 14
0
votes
5 answers

I need to remove duplicate products in an Array and sum their quantity in JavaScript

In the program, I fetch arrays of products from orders via API. The array contains a list with products that are duplicated. I need to remove the duplicate products in the array and sum their quantities. This is what the array I get looks…
iNeeR
  • 9
  • 1
0
votes
0 answers

What is going on with the accumulator in this example from MDN?

So I was going through the MDN docs on the .reduce method in JavaScript and I was having a hard time understanding what is going on with the accumulator (acc). I was expecting the accumulator to populate/group according to age as it looped through…
corn
  • 9
0
votes
1 answer

How to run multiple timers one after another with pause in javacript?

I need to run multiple timer one after another with pause. Input for timer comes from the array which contains set of times. I did something with reduce method. But could not break early the reduce method. const times = [5, 4, 5]; function…
Shiva C
  • 30
  • 6
0
votes
4 answers

Combine objects in array if dates are equal javascript

I have an array in Javascript and I need to combine objects if they have equal reminder.date const tasks = [ { id: 1, title: "call Tom", reminders: { date: "2022-02-01", time: "09:30" } }, { id: 2, title: "Meet Bred", reminders: { date:…
0
votes
1 answer

PHP - Sum Array key value where 2 or more other keys are equal while keeping array structure

I have this source array(): $source[] [ ["user_id": 1, "item_id": 991, "quantity": 100], ["user_id": 1, "item_id": 992, "quantity": 50], ["user_id": 1, "item_id": 993, "quantity": 300], ["user_id": 1, "item_id": 992, "quantity":…
Bo. Ga.
  • 138
  • 1
  • 4
  • 12
0
votes
2 answers

Aggregate array of object to single distinct value of object javascript

I want to aggregate array of object into single object with each object having distinct value. But My code is not giving me desired output. Can someone please help me? My Input : [{ "obj1" : "value1", "obj2" : ["abc", "def"], …
Samira
  • 71
  • 12