最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - find minimum and maximum value in attribute using jquery - Stack Overflow

programmeradmin3浏览0评论

I need to find minimum and maximum value in data attribute but it returns 0

here is my code

<div data-price="2" class="maindiv">test</div>
<div data-price="5" class="maindiv">test</div>
<div data-price="3" class="maindiv">test</div>
<div data-price="0" class="maindiv">test</div> 
<div data-price="25" class="maindiv">test</div>     
<div data-price="10" class="maindiv">test</div> 

<script>
    var ids = $("div[data-price]").map(function() {
        return this.id;
    }).get();

    var highest = Math.max.apply( Math, ids );
    var lowest = Math.min.apply( Math, ids );

    alert(highest);
    alert(lowest);
</script>

I need to find minimum and maximum value in data attribute but it returns 0

here is my code

<div data-price="2" class="maindiv">test</div>
<div data-price="5" class="maindiv">test</div>
<div data-price="3" class="maindiv">test</div>
<div data-price="0" class="maindiv">test</div> 
<div data-price="25" class="maindiv">test</div>     
<div data-price="10" class="maindiv">test</div> 

<script>
    var ids = $("div[data-price]").map(function() {
        return this.id;
    }).get();

    var highest = Math.max.apply( Math, ids );
    var lowest = Math.min.apply( Math, ids );

    alert(highest);
    alert(lowest);
</script>
Share Improve this question edited Jun 13, 2016 at 11:54 krlzlx 5,82114 gold badges49 silver badges59 bronze badges asked Jun 13, 2016 at 10:28 vellai duraivellai durai 1,0113 gold badges18 silver badges41 bronze badges 1
  • Did these answers provide solution to your problem? – Anoop Joshi P Commented Jun 13, 2016 at 10:57
Add a ment  | 

3 Answers 3

Reset to default 7

You need to return data-price attribute, not its id

var ids = $("div[data-price]").map(function() {
    return $(this).attr("data-price");
}).get();

var highest = Math.max.apply( Math, ids );
var lowest = Math.min.apply( Math, ids );

alert(highest);
alert(lowest);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-price="2" class="maindiv">test</div>
<div data-price="5" class="maindiv">test</div>
<div data-price="3" class="maindiv">test</div>
<div data-price="0" class="maindiv">test</div> 
<div data-price="25"class="maindiv">test</div>     
<div data-price="10" class="maindiv">test</div>

You can do this with regular JavaScript:

//Variable to hold our results
var data = [];
//Loop through HTML nodes
for (var i = 0; i < document.querySelectorAll(".maindiv").length; i++) {
	data.push({
		node : document.querySelectorAll(".maindiv")[i],
		value : parseInt(document.querySelectorAll(".maindiv")[i].getAttribute("data-price"))
	});
}
//Sort lowest to highest (Only use one sort! Pick either this or the next one as they overwrite each Other)
data = data.sort(function (a, b) {
		return a.value - b.value;
	});
//Sort highest to lowest (Only use one sort! Pick either this or the last one as they overwrite each Other)
data = data.sort(function (a, b) {
		return b.value - a.value;
	});
//List all
console.log(data);
//List first (highest or lowest depending or sort)
console.log('first', data[0].value);
//List last (highest or lowest depending or sort)
console.log('last', data[data.length-1].value);
<div data-price="2" class="maindiv">test</div>
<div data-price="5" class="maindiv">test</div>
<div data-price="3" class="maindiv">test</div>
<div data-price="0" class="maindiv">test</div> 
<div data-price="25" class="maindiv">test</div>     
<div data-price="10" class="maindiv">test</div>

Currently you are returning the id attribute from the map function. Change it to to data-price

var ids = $("div[data-price]").map(function() {
  return $(this).data("price");
}).get();
var highest = Math.max.apply( Math, ids );
var lowest = Math.min.apply( Math, ids );
alert(highest)
alert(lowest)

Fiddle

发布评论

评论列表(0)

  1. 暂无评论