I have an array of objcets. With reduce method after looping i want to receive two seperate arrays. Now, i am doing this way.
const dogs = [
{ weight: 22, curFood: 250, owners: ['Alice', 'Bob'] },
{ weight: 8, curFood: 200, owners: ['Matilda'] },
{ weight: 13, curFood: 275, owners: ['Sarah', 'John'] },
{ weight: 32, curFood: 340, owners: ['Michael'] },
];
So, with the below reduce methods i am receiving the result that i want (2 array).
const ownersEatTooMuch = dogs.reduce(function (acc, dog) {
if (dog.curFood > dog.recommended * 1.1) {
acc.push(dog.owners);
}
return acc.flat();
}, []);
const ownersEatTooLess = dogs.reduce(function (acc, dog) {
if (dog.curFood < dog.recommended * 0.9) {
acc.push(dog.owners);
}
return acc.flat();
}, []);
But is it possible to merge this into one reduce method to create 2 arrays. I imagine situation like this,
const [ownersEatTooMuch1, ownersEatTooLess1] = dogs.reduce(function (dog) {
// When the condition will be true i want to fill first array ownersEatTooMuch1 and when another condition will be true i want to fill second array ownersEatTooLess1
}, [[], []]);
const [ownersEatTooMuch1, ownersEatTooLess1] = dogs.reduce(
function (dog) {
if (dog.curFood > dog.recommended * 1.1) {
acc.push(dog.owners);
}
},
[[], []]
);
I just don’t understand how to determine [[], [] between these and then push into ownersEatTooMuch1 or here ownersEatTooLess1
7
Answers
I hope this is what you are looking for.
Because your example code do not work (
recommended
undefined), and the output is not clear, im not sure if this is what you want.Based on the code you provided you want 2 arrays with "ate too much" or "ate too less".
What about the cases which is neither "too much" or "too less"?
See my comment in the array definition:
does not apppear anywhere, neither "too much" or "too less"
The reduce iteration returns every time the initial array passed to it, which is a 2 layer array.
These arrays get then filled based on your calculation.
The 2 layer array returned from reduce is then deconstructed into the variables
ownersEatTooMuch
&ownersEatTooLess
.it’s easy. You can do something like that:
You should just update the result key to avoid duplication code ( i don’t know your specific logic)
I think what you need is the
...
(spread) operator.Array push
Array push with spread syntax
You can use
push
with the spread syntax to add elements to each array:Array push with Iteration
You can use
push
and then iterate through the array ofowners
instead of usingflat
:Array concat
You can use
reduce
with the Arrayconcat
method to create two arrays, each iteration may create a new array since concat is not an in-place destructive operation:Sure:
Explanation:
tooMuch
andtooLess
else if
, since we exclude someone eating too much and too less at the same timeacc
tooMuch
andtooLess
, both being arraysI have added a
recommended
field to each dog so the comparisons will make sense.Not the reduction you were asking for, but you could use one expression involving
filter
andflatMap
:This is not the fastest running code, but could be perceived as "elegant" (a matter of opinion).
Note that it will deal differently when the ratio is exactly 1.1. In that case the owners will also be added in the "Too much" group. I assume this is not a problem. You can always adjust the factor to your liking.
Secondly, the sloppy
==
is intended here. If you prefer using===
, then convert the left operand to number with a unary+
operator.The reducer of this snippet delivers an array with 3* subarrays. The reducer result is spreaded into 3 variables.
I had to guess theI used the function you gave in the comment section of your question to setrecommended
value for the demorecommended
values.* Why 3? Because I guess there may be ‘undecided’ values, given the comparison you gave.