# Tag: reduce

## How to count repeating value inside an array of object

Given the array of obj above, My goal is to get the total number of vehicle in each key. Note: if the vehicle_name is repeated it should only count as 1. Expected Output: Explanation: The count of key a => is 2 because although it have 4 vehicles but Toyota is repeated so it only count as 1. Same goes

## How to group according to date{ the key in each object is the day – from the first day of the month untill the current date}

Given the array above, my goal is to return an array of object that are group according to date(On Rent). But first I need to get the start of the month (for example this month is september, and also the current date of this month so for example Sept 25) so my final output should be like this Also I

## How to proper use reduce() javascript/typescript

I try to solve task below with reduce() and actually the result is ok. But I don’t know how to use acc in reduce() instead of acc1 or indexMax. How to apply typescript to this task. The task: Find first word with max repeated letter count. For example rrrtygggg and ttbvfddjklyyyaseqq – the winner is rrrtygggg. If sequence has no

## How to write this function with forEach?

I am trying to group the people by their age and as you can see, my code works with the reduce method. I managed to get this working but now I want to do the same with forEach method. Here’s my code: Answer Reduce accepts initial value that you update each iteration and return. So if you wish to use

## Reduce nested array maximum values to a single array

I am trying to find a way to reduce the max values of specific positions/indexes within nested arrays into a single array. Given: const myArr = [[105,87,171],[113,192,87],[113,87,87],[113,87,87]] Expected return value: [113,192,171] Actual return value: [113,87,87] Using the following code, I’m getting the above return value. Why am I only getting the max value for the first position, and not the

## Looping through and reducing nested array

I can’t seem to get my head around the array.reduce() function. I’ve got the following array: and I’m trying to get a result like below. I’m not sure how to go about multiplying the ingredient amounts with the item quantities: I’ve tried following the structure that was given here but the arrow function is throwing me off. Any sort of

## Javascript reduce() on array of objects, with entries being simple variables and array as well

I’m trying to use the reduce function in order to iterate through an array of objects and to get an output summing up the data shown inside of the array of objects (a kind of duplicate remover in fact). On data such as : when using the function : It will fork fine if I call this getValues function getValues(mainData,

## 4th argument in reduce

Here is the function: The fourth argument to reduce is the array that we’re iterating over. I can log it and I get the correct result (10) see above. But when I try to use it and assign it to a variable I get an error(see above). Could someone please shed some light? Answer From Mozilla’s page, the fourth parameter

## Compare objects within an array and create a new array without duplicates

I’ve an array of objects: I want to compare the objects within an array by name. If the duplicate name exists, I should compare the age and only keep the higher age object. The expected output should be: I am new to javascript/typescript and couldn’t find any optimal solution for this problem. I hope, I was able to explain my

## how to alter reduce function so it produce different result

I have this array: I want to transform that array into I have this code that doing about that: What it missing is ignoring the sections. Any ideas how to modify that reduce function so it will produce expected result? To determine a section I came up with this funciton: Answer I would use the same approach from my answer