Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
These three versions of equivalent code demonstrates how code should be self-explainable
dataArray.reduce((a, b) => [...a,
...(b.data && b.data.isImportant && b.data.value ? [b.data.value] : [])
], []);
/**
* Thwe horrific JavaScript snippet above is equivalent to the following
*/
dataArray
.filter((x) => x.data && x.data.isImportant && x.data.value)
.map((x) => x.data.value);
/**
* And if you really insist on a single iteration, meaningful names
* improve readability significantly
*/
dataArray.reduce((result, element) => {
if (element.data) {
const { isImportant, value } = element.data;
if (isImportant && value) return [...result, value];
}
return result;
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment