58 questions
0
votes
0
answers
42
views
Optimised Javascript/TS code to find the total quantity for each base item, when more than 1 cart item can be pointing to the same base item
I need the most efficient way to calculate the total quantity for each base item by going through a list of cart items and sum up the quantities for all cart items having the same base item.
My ...
-3
votes
1
answer
105
views
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 ...
0
votes
1
answer
76
views
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 ...
0
votes
1
answer
93
views
How can I assign properties to an object in php specifically using array_reduce? [duplicate]
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
$...
1
vote
0
answers
47
views
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', ...
0
votes
1
answer
119
views
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". ...
0
votes
1
answer
243
views
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,
...
-1
votes
4
answers
2k
views
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' },
{
...
1
vote
1
answer
70
views
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.
...
0
votes
3
answers
95
views
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,
...
}
...
0
votes
1
answer
1k
views
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 ...
0
votes
0
answers
23
views
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>(): [
T,
({ key, value }: { key: keyof T; value: string }) ...
1
vote
1
answer
870
views
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 (...
3
votes
6
answers
14k
views
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 ...
0
votes
5
answers
2k
views
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 ...
0
votes
0
answers
21
views
What is going on with the accumulator in this example from MDN? [duplicate]
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 ...
0
votes
1
answer
311
views
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 ...
0
votes
4
answers
94
views
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",
...
2
votes
3
answers
597
views
Array of objects - how to group items by property and squash data from each of grouped items into 1
I am trying to perform operation on array that is described in topic.
What I've done already - group by common property + map data of each item to desired format. It was achieved by using reduce + map ...
0
votes
1
answer
325
views
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":...
2
votes
2
answers
1k
views
Getting sum of total value from Reduce
I have 3 label that can receive input when button clicked
and then i need sum from the third input value
the code kinda work
but instead of getting the sum of the third input it instead it looks like ...
1
vote
1
answer
289
views
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 = [
{ ...
1
vote
2
answers
106
views
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&...
0
votes
2
answers
637
views
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 :
[{
"...
0
votes
1
answer
73
views
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 =
...
0
votes
2
answers
193
views
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};
// ...
-2
votes
1
answer
957
views
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 &...
0
votes
2
answers
72
views
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": "...
0
votes
1
answer
539
views
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()
<?php
$users=$this->db->get('...
1
vote
1
answer
48
views
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:
...
1
vote
1
answer
285
views
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 ...
2
votes
5
answers
237
views
Javascript array restructuring for nested array
I have an array of objects as mentioned below.
const inputArray =[
{
name: "Energy",
quantity: [
{
qval: "100 ",
unit: "unit1"
},
{
qval: "200 ",
...
2
votes
1
answer
546
views
Condition inside array_reduce
I want to make condition out of the following array, but it does not give my expected result because it does not run the second condition.
$arr = [
[
472 => [
'EL' =&...
-1
votes
3
answers
572
views
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 ...
1
vote
3
answers
3k
views
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 ...
3
votes
5
answers
1k
views
Sum each row's deep values in a 3-dimensional array
I have need to calculate the sum of the deep elements foreach first level element.
Sample 3-level array:
[
1 => [
'A' => ['AA' => 3, 'AB' => 5],
'B' => ['BA' => 2]...
-1
votes
3
answers
4k
views
find the minimum and maximum value in array group php
Array
(
[0] => Array
(
[package] => LTE_15AGB
[value] => Array
(
[0] => 52690
[1] => 24700
...
0
votes
4
answers
524
views
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, ...
1
vote
3
answers
2k
views
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 ...
0
votes
5
answers
2k
views
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 },
{ ...
1
vote
1
answer
5k
views
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
...
1
vote
1
answer
481
views
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], $...
0
votes
1
answer
3k
views
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
vote
2
answers
62
views
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, $...
13
votes
18
answers
15k
views
How to convert multi-dimensional array into single array using PHP?
After implementing database queries, I am getting the multi-dimensional array below.
Two Dimensional Array
Array
(
[0] => Array
(
[t1] => test1
)
[1] => ...
2
votes
1
answer
2k
views
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 ...
-5
votes
1
answer
100
views
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: ...
3
votes
2
answers
4k
views
JavaScript code to get count of occurrence of objects in array of objects using array.reduce()
var developers = [
{ name: "Joe", age: 23 ,overallLevel: "high"},
{ name: "Sue", age: 28 ,overallLevel: "advanced" },
{ name: "Jon", age: 32 ,overallLevel: "high" },
{ name: "Bob", age:...
4
votes
1
answer
1k
views
Multiplying array indexes in PHP with array_reduce
Why does the array_reduce() method work differently when adding and multiplying? When I add the array values below, the code produces the expected result: 15. But when I multiply, it returns: 0. Same ...
1
vote
1
answer
1k
views
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
(
[...