Skip to content

Instantly share code, notes, and snippets.

@Naedri
Created August 6, 2022 07:28
Show Gist options
  • Save Naedri/69e04742430341935a6f0bd4e9b0078d to your computer and use it in GitHub Desktop.
Save Naedri/69e04742430341935a6f0bd4e9b0078d to your computer and use it in GitHub Desktop.
Simple example of reduce function.
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