0%

jQuery对数组key进行A-Z排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var options={};

//获取每一个选项的显示值,保存到options对象中
$('#myselect option').each(function(){
var option = $(this);
options[option.text()] = option;
});

var keys = Object.keys(options).sort();

var $myselect = $('#myselect').empty();

$.each(keys,function(i,key){
$myselect.append(options[key]);
});

我开发的项目中使用的代码例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function Abc(){
var abc = Array();
jQuery('.pro_list .pro_data.sho.sho1.sho2.sho3.sho4.sho5.sho6.sho7.sho8.sho9.sho10.sho11.sho12.sho13.sho14.sho15.sho16.sho17.sho18.sho19.sho20').each(function(index, element) {
var cont = jQuery('.pro_list .pro_data.sho.sho1.sho2.sho3.sho4.sho5.sho6.sho7.sho8.sho9.sho10.sho11.sho12.sho13.sho14.sho15.sho16.sho17.sho18.sho19.sho20:eq(' + index + ')').html();
var p_title = jQuery('.pro_list .pro_data.sho.sho1.sho2.sho3.sho4.sho5.sho6.sho7.sho8.sho9.sho10.sho11.sho12.sho13.sho14.sho15.sho16.sho17.sho18.sho19.sho20:eq(' + index + ') .pro_name').text();
abc[p_title] = cont;
});

var keys = Object.keys(abc).sort();
jQuery(".pro_list .pro_data.sho.sho1.sho2.sho3.sho4.sho5.sho6.sho7.sho8.sho9.sho10.sho11.sho12.sho13.sho14.sho15.sho16.sho17.sho18.sho19.sho20").empty();
jQuery.each(keys, function(i, key){
console.log(abc[i]);
jQuery('.pro_list .pro_data.sho.sho1.sho2.sho3.sho4.sho5.sho6.sho7.sho8.sho9.sho10.sho11.sho12.sho13.sho14.sho15.sho16.sho17.sho18.sho19.sho20:eq(' + i + ')').append(abc[key]);
});
}