Как из строки достать все числа?
В общем. Есть страничка с товарами и ценами, нужно достать все цены и определить максимальную и минимальную, проблема состоит в том что бы вытащить цены из строки.
В HTML блоки с ценами выглядят так: <div class="price">2000 грн</div> <div class="price">1000 грн</div> <div class="price">3000 грн</div> <div class="price">5000 грн</div> ... В JQ получается вот такая строка: var price = $('.price').text(); //2000 грн1000 грн3000 грн5000 грн ... Как из такой строки получить минимальное и максимальное число? |
miusov,
<!DOCTYPE html> <html> <head> <title>Untitled</title> <meta charset="utf-8"> <style type="text/css"> </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(function() { var min,max; $('.price').text(function(i,t) { t = +t.replace(/\D/g,''); (!min|| t < min)&&(min=t); (!max|| t > max)&&(max=t); }) alert([min,max]) }); </script> </head> <body> <div class="price">2000 грн</div> <div class="price">1000 грн</div> <div class="price">3000 грн</div> <div class="price">5000 грн</div> </body> </html> |
jQuery(function ( $ ) { var prices = $('.price').text().match(/\d+/g).sort(), min = prices[0], max = prices[ prices.length - 1 ]; }); |
Цитата:
<!DOCTYPE html> <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=windows-1251' /> <script src='http://code.jquery.com/jquery-latest.js'></script> <!-- <script src="https://code.angularjs.org/1.3.9/angular.min.js"></script> <script src="https://code.angularjs.org/1.3.9/angular-route.js"></script> --> <style type='text/css'> </style> <script type='text/javascript'> $(function(){ var price = $('.price').text().match(/\d+/g).sort(); alert('min: '+price[0]+' max: '+price[price.length-1]) }); </script> </head> <body> <div class="price">2000 грн</div> <div class="price">1000 грн</div> <div class="price">3000 грн</div> <div class="price">5000 грн</div> </body> </html> |
Опередили... :D
|
dd_smol,
нужна функция сортировки! иначе цифры отсортируются как строки. <!DOCTYPE html> <html> <head> <title>Untitled</title> <meta charset="utf-8"> <style type="text/css"> </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(function() { var min,max; var prices = $('.price').text().match(/\d+/g).sort(), min = prices[0], max = prices[ prices.length - 1 ]; alert([min,max]) }); </script> </head> <body> <div class="price">2000 грн</div> <div class="price">1000 грн</div> <div class="price">3000 грн</div> <div class="price">9 грн</div> </body> </html> |
ksa,
тоже самое |
Цитата:
|
:write:
<!DOCTYPE html> <html> <head> <title>Untitled</title> <meta charset="utf-8"> <style type="text/css"> </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(function() { var min,max; var prices = $('.price').text().match(/\d+/g).sort(function(a,b) { return a - b }), min = prices[0], max = prices[ prices.length - 1 ]; alert([min,max]) }); </script> </head> <body> <div class="price">2000 грн</div> <div class="price">1000 грн</div> <div class="price">3000 грн</div> <div class="price">9 грн</div> </body> </html> |
Точно есть такой не приятный момент. :)
Тогда немного до делаем: jQuery(function ( $ ) { var prices = $('.price').text().match(/\d+/g).sort(function (a, b) { return a - b; }), min = prices[0], max = prices[ prices.length - 1 ]; }); |
Часовой пояс GMT +3, время: 14:02. |