Created
August 6, 2022 07:28
-
-
Save Naedri/69e04742430341935a6f0bd4e9b0078d to your computer and use it in GitHub Desktop.
Simple example of reduce function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const totalAdresse = docs.reduce((prevValue, contrat) => { | |
console.log(prevValue, contrat); | |
if (prevValue.includes(contrat.adresse)) { | |
return prevValue; | |
} else { | |
return [...prevValue, contrat.adresse]; | |
} | |
}, []); | |
const totalAchatAndVente = docs.reduce( | |
(prevValue, contrat) => { | |
if (contrat.achatDevise === hash) { | |
return { | |
...prevValue, | |
totalAchat: prevValue.totalAchat + contrat.achatAmount, | |
contratsAchat: [...prevValue.contratsAchat, contrat], | |
}; | |
} | |
if (contrat.venteDevise === hash) { | |
return { | |
...prevValue, | |
totalVente: prevValue.totalVente + contrat.venteAmount, | |
contratsVente: [...prevValue.contratsVente, contrat], | |
}; | |
} | |
return prevValue; | |
}, { | |
totalAchat: 0, | |
totalVente: 0, | |
contratsAchat: [], | |
contratsVente: [] | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment