Skip to content

Instantly share code, notes, and snippets.

@SanjeQi
Created April 23, 2019 17:42
Show Gist options
  • Save SanjeQi/9496d395efe2daef6b997daadde61986 to your computer and use it in GitHub Desktop.
Save SanjeQi/9496d395efe2daef6b997daadde61986 to your computer and use it in GitHub Desktop.
Get the average pixels
function getAveragePixels(cssObj) {
return Object
.values(cssObj)
.filter(el => el.includes('px'))
.map(el => parseInt(el))
.reduce((acc, nr, index, array) => acc = (acc + nr) / array.length)
}
getAveragePixels({
marginLeft: '6px',
marginBottom: '4px',
overflow: 'hidden',
position: 'fixed'
}) === 5;
getAveragePixels({
paddingTop: '10px',
display: 'block',
paddingLeft: '20px'
}) === 15;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment