Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Как из строки достать все числа? (https://javascript.ru/forum/jquery/64664-kak-iz-stroki-dostat-vse-chisla.html)

miusov 28.08.2016 12:42

Как из строки достать все числа?
 
В общем. Есть страничка с товарами и ценами, нужно достать все цены и определить максимальную и минимальную, проблема состоит в том что бы вытащить цены из строки.

В 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 грн ...


Как из такой строки получить минимальное и максимальное число?

рони 28.08.2016 13:07

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>

dd_smol 29.08.2016 10:34

jQuery(function ( $ ) {
	var prices	= $('.price').text().match(/\d+/g).sort(),
		min		= prices[0],
		max		= prices[ prices.length - 1 ];
});

ksa 29.08.2016 10:43

Цитата:

Сообщение от miusov
Как из такой строки получить минимальное и максимальное число?

Как вариант...
<!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>

ksa 29.08.2016 10:44

Опередили... :D

рони 29.08.2016 10:45

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>

рони 29.08.2016 10:45

ksa,
тоже самое

ksa 29.08.2016 10:47

Цитата:

Сообщение от рони
нужна функция сортировки! иначе цыфры отсортируются как строки

Про это забыл... И пример не имеет таких данных. :(

рони 29.08.2016 10:49

: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>

dd_smol 29.08.2016 10:53

Точно есть такой не приятный момент. :)
Тогда немного до делаем:
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, время: 10:39.