Reputation: 9293
I want to do something if all .cimg
have the same width.
<img class='cimg' src='...' alt='img'>
<img class='cimg' src='...' alt='img'>
<img class='cimg' src='...' alt='img'>
js
let w = $.map($('.cimg'), (e) => $(e)[0].naturalWidth);
result - [960, 960, 960]
if(all elements of w are equal){do_something();} // how to do this?
Upvotes: 1
Views: 49
Reputation: 68933
You can use [...new Set()]
to get all the unique items from the array, then simply check the length
of the resulted array. If the length is 1
then all width are same otherwise not:
let w = $.map($('.cimg'), (e) => $(e)[0].naturalWidth);
w = [...new Set(w)]
if(w.length == 1){console.log('same');}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class='cimg' src='...' alt='img'>
<img class='cimg' src='...' alt='img'>
<img class='cimg' src='...' alt='img'>
Upvotes: 3