javascript jquery中.Max()的等价物是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7307173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:41:52  来源:igfitidea点击:

What is the equivalent of .Max() in jquery

javascriptjquerylinqmax

提问by Mo Valipour

I was wondering how can we write a jquery statement to get the max value of a property of matched elements.

我想知道我们如何编写一个 jquery 语句来获取匹配元素的属性的最大值。

in LINQ we say something like this:

在 LINQ 中,我们这样说:

var maxHeight = list.Max(a=> a.Height);

what is the best way to do this in jquery?

在 jquery 中执行此操作的最佳方法是什么?

as an example let's say we want to select the max numeric value of all :text elements inside a container:

例如,假设我们要选择容器内所有 :text 元素的最大数值:

var allInputs = $("#container :text");
// how to get the max of parseInt(item.val())?

of course having a loop over all elements is an option but I'm curious to know if there is a magic to do with jquery.

当然,对所有元素进行循环是一种选择,但我很想知道 jquery 是否有魔力。

Regards.

问候。

采纳答案by Mo Valipour

Based on all the conversation here and my search in the net we can say that (at this time) there is no robust built-in jquery way to calculate .max() and .min() of a property of elements matching a selector.

基于这里的所有对话和我在网上的搜索,我们可以说(此时)没有强大的内置 jquery 方法来计算与选择器匹配的元素的属性的 .max() 和 .min() 。

From what Igor suggested I came up with the following two jquery functions which you can add to your project if you need to use .max() and .min() functionalities over and over:

根据 Igor 的建议,我想出了以下两个 jquery 函数,如果您需要反复使用 .max() 和 .min() 功能,您可以将它们添加到您的项目中:

$.fn.max = function(selector) { 
    return Math.max.apply(null, this.map(function(index, el) { return selector.apply(el); }).get() ); 
}

$.fn.min = function(selector) { 
    return Math.min.apply(null, this.map(function(index, el) { return selector.apply(el); }).get() );
}

// Usage:

var maxWidth = $("a").max(function() {return $(this).width(); });
var minWidth = $("a").min(function() {return $(this).width(); });

See a demo here: http://jsfiddle.net/NF7v7/3/

在此处查看演示:http: //jsfiddle.net/NF7v7/3/

回答by Igor Dymov

One of the possible solutions is to use mapfunction and then Math.maxto the result:

可能的解决方案之一是使用map函数,然后使用Math.max结果:

var values = $.map($("input:text"), function(el, index) { return parseInt($(el).val()); });
var max = Math.max.apply(null, values);

If it is needed it might be written in one line.

如果需要,可以写成一行。

fiddle: http://jsfiddle.net/WuVLr/1/

小提琴:http: //jsfiddle.net/WuVLr/1/

UPDATE:

更新:

Alternatively you can apply mapin this way:

或者,您可以map通过以下方式申请:

var values = $("input:text").map(function(index, el) { return parseInt($(el).val()); }).get();

回答by NYTom

I am the author of the open source project http://www.jinqjs.comUsing jinqJs you could do the following:

我是开源项目http://www.jinqjs.com的作者 使用 jinqJs 您可以执行以下操作:

You could get the max by doing this:

你可以通过这样做获得最大值:

var result = jinqJs().from([2,5,4,6,7,8,3]).orderBy([{sort: 'desc'}]).top(1).select();  

or the min by doing this:

或 min 这样做:

var result = jinqJs().from([2,5,4,6,7,8,3]).orderBy([{sort: 'desc'}]).bottom(1).select();  

回答by GoRoS

This could be a non-direct solution for that using an array:

这可能是使用数组的非直接解决方案:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                var mySizes=new Array();
                $("input[type=text]").each(
                   function( intIndex ){
                      mySizes[intIndex] = $(this).val().length;
                   });
                var largest = mySizes.sort(function(a,b){return a - b}).slice(-1);
                alert("The maximun length is: " + largest);
            });
        </script>
    </head>
<body>
    <input type="text" value="Test"id="1"/>
    <input type="text" value="Goodbye"id="2"/>
    <input type="text" value="Bye"id="3"/>
    <input type="text" value="Hello"id="4"/>
    <input type="text" value="Hello world!"id="5"/>
    <input type="text" value="Hi"id="6"/>
</body>
</html>

Regards

问候