jQuery each always sort it?

前端 未结 4 495
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 13:03

I have this object in JS:

var list = {134 : \"A\",140 : \"B\",131 : \"C\"}

I run it with:

jQuery.each(list, function(key, v         


        
4条回答
  •  离开以前
    2020-12-20 13:44

    I bump into your question, and it took me 10 min to understand.

    This is how you would solve your problem :

    var list = {134 : "A",140 : "B",131 : "C"};
    
    // 1 - property to list
    list = Object.keys(list).map(
        function(key) {
             return { num : key , char : list[key]};;
         });
    
    console.debug(list);
    // 2 - sorting the list
    var sorted = list.sort(function(a, b) {
                if(a.char < b.char) return -1; return 1;
            });
    
    // 3 output
    jQuery.each(sorted, function(index, obj) {      
            console.log(obj.num + " - " + obj.char);
    });
    

    JsFiddle: https://jsfiddle.net/wx38rz5L/1578/

提交回复
热议问题