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

recommendedis never defined anywhere..reduce()asdogs.filter(dog => dog.curFood > dog.recommended * 1.1).flatMap(({ owners }) => owners)