English 中文(简体)
jquery - 如何在四个阵列中选择最长的id和最后元素?
原标题:jquery - how to select the id and last element of the longest out of four arrays?

我的网页上有4个DIV要素。 每个委员会都有自己的阵列,我随附使用数据。

视用户互动(页面导航)而定,仓储在其中一个阵列中装载了元件识别器。

构造和推导元素是行之有效的。

$( div:jqmData(role="panel") ).each(function(index) {
    if ( $(this).jqmData( hash ) ==  history  ) {
         var id = $(this).jqmData( id );
         $(this).data("stack", []);
         // inital entry = startpage
         $(this).data("stack").push( # +$(this).find( .starter ).attr( id )); 
         }
}); 

添加内容:

$( div:jqmData(role="page") ).bind( "pagechange", function( event) {
      var $targetPanel = $( event.target ),                 
      $targetPage =  # + $.mobile.path.stripHash( hash );   
      $targetPanel.data("stack").push($targetPage);
      });

我现在需要知道哪一个阵列是最长的,最后一个条目是什么?

......

t 最大程度 数值或数学,因为我只有像ppageSome, #page 其他条目。 必须要有更好的办法,而不是通过所有阵列,使其时间长,努力达到高价值。

帮助!

最佳回答

你们能够利用这种方法找到最长久的阵容。

var arr = [];
arr.push([1,2,3,4,5,6]);
arr.push([1,2,3,4,5,7,8]);
arr.push([1,2,3,4,5]);
arr.sort(function(a,b){return b.length - a.length});
// arr is now sorted with [0]  being the longest array

然后,为了对这些阵列进行分类,你可以发挥类似的作用:

$( div:jqmData(role="panel"),div:jqmData(role="page") ).each(function(index) {
    arr.push($(this).data("stack"));
});

或者,如果阵列中的数据不经常变动,那么,你就可以推动各阵列,因为它们是在上述两项职能中建立的。

<EDIT 通过 发表最新信息

Then when you want to reference the longest array and use it as a jQuery selector, you can use the following code:

var longest = arr[0].join(",") // arr was sorted to have the first index contain the longest array
$(longest).hide();
问题回答
var longestArrayStack = function() {
    var l = Array.prototype.sort.call(arguments, function(a, b) {
        return b.length - a.length;
    });
    return l[0];
});

然后:

var arr = [];
$( div:jqmData(role="page") ).each(function(){
    arr.push( $(this).data("stack") );
});
var longestArr = longestArrayStack(arr);




相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签