JQuery Javascript Sort Array by Highest Count

后端 未结 5 521
清酒与你
清酒与你 2021-01-23 06:54

I have:

myArray = [\"ABAB\", \"ABAB\", \"ABAB\", \"CDCD\", \"EFEF\", \"EFEF\"]

I need to count by occurrences and sort by highest count. This w

5条回答
  •  孤街浪徒
    2021-01-23 07:34

    see console in firefox to see the resulting object

    http://jsfiddle.net/4dqeK/1/

    myArray = ["ABAB", "ABAB", "ABAB", "CDCD", "EFEF", "EFEF"];
    
    container={};
    
     for(var i=0; i < myArray.length;i++){
            var el=myArray[i];
            if( el in container){
                container[el].push(el);
            }
            else{
                container[el]=[];
                container[el].push(el);
            }
    
    
        }
        console.log(container);
    

提交回复
热议问题