我之前发布过关于在 javascript 中对对象进行排序的文章。对于无法对对象进行排序,我有一个很大的禁忌。
我被告知的路径是将对象转换为数组。
我已将 totalScore 声明为一个新数组,并对其应用降序函数。- 这不起作用。任何想法为什么?
这是代码(我正在开发一个井字游戏):
var data = {
"arr0" : ["1","1","1","0","0","0","0","0","0"],
"arr1" : ["1","0","0","0","1","0","0","0","1"],
"arr2" : ["0","0","0","1","1","1","0","0","0"],
"arr3" : ["0","0","0","0","0","0","1","1","1"],
"arr4" : ["0","0","1","0","1","0","1","0","0"],
"arr5" : ["1","0","0","1","0","0","1","0","0"],
"arr6" : ["0","1","0","0","1","0","0","1","0"],
"arr7" : ["0","0","1","0","0","1","0","0","1"]
};
function win(){
var pocket,
totalScore = new Array,
score,
maxM,
i;
jQuery.each(data, function(d_key, d_val){
score = 0;
i = 0;
jQuery.each(d_val, function(key, val){
pocket = jQuery(document).find('[data-kcpos="'+key+'"]');
if(val == 1 && jQuery(pocket).hasClass("blank")){
score += 1;
} else if(val == 1 && jQuery(pocket).text() == "x"){
score += 0;
} else if(val == 1 && jQuery(pocket).text() == "0"){
score += 2;
}
if(val == 1) i++;
if(i == 3) return false;
});
totalScore[d_key] = score;
});
function descending( a, b ) {
return b - a;
}
totalScore.sort( descending );
}