简体   繁体   中英

find minimum and maximum value in attribute using jquery

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>

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.com/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM