Created
January 12, 2022 14:03
-
-
Save nxvhm/69d7b2aedf0e74920541b60e26fdb6a5 to your computer and use it in GitHub Desktop.
Order array of urls of images by their dimensions
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
| let orderbyDimensions = images => { | |
| let promises = []; | |
| // console.log('to order by dimensions', images); | |
| images.forEach(imgUrl => { | |
| let img= new Image; | |
| img.src = imgUrl; | |
| let promise = new Promise( (resolve, reject) => { | |
| img.onload=function(){ | |
| resolve({with: this.width, h:this.height, max: this.width*this.height, url: imgUrl}); | |
| }; | |
| }); | |
| promises.push(promise); | |
| }); | |
| Promise.all(promises).then(values => { | |
| values.sort((a, b) => { | |
| if (a.max < b.max) return 1; | |
| if (a.max > b.max) return -1; | |
| }); | |
| console.log('resolved and sorted', values); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment