JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

1,371,533 Responses

  1. First of all I want to say great blog! I had a
    quick question which I’d like to ask if you do not mind.
    I was interested to know how you center yourself and clear your thoughts before
    writing. I have had a difficult time clearing my mind in getting my
    ideas out there. I truly do enjoy writing however it just seems like the
    first 10 to 15 minutes are lost simply just trying to figure
    out how to begin. Any suggestions or hints?
    Many thanks!

  2. What Are The Reasons You Should Be Focusing On Making Improvements To Asbestos Mesothelioma mesothelioma attorneys

  3. Jean Hoffman表示:

    Highlighting warranties in roofing is key—always ask about them before making a decision! roofing company

  4. CharlesNom表示:

    Photograph collages have long been cherished for their ability to capture some sort of spectrum of recollections. However, individualized photo collages take this specific concept to the new level by simply tailoring images to tell an unique, personal story. These kinds of customized collections weave together cherished times, developing a visual narrative that resonates seriously using the individual or group they signify.

    Beauty of an individual collage lies within its capability to stir up emotion. By thoroughly selecting photographs of which hold personal value, the collage becomes a tangible reflection involving one’s journey, milestones, and experiences. It’s more than merely a decoration—it’s some sort of living memory, taken in time.

    https://jaidenddqcp.blogdomago.com/25481807/unlocking-reminiscences-the-great-thing-about-individualized-photograph-collages

    In addition, individualized collages help to make excellent gifts, allowing recipients to re-experience their most cherished moments. Whether viewed at home or business office, these collages certainly not only offer cosmetic appeal and also serve as powerful reminders of the associations, adventures, and milestones that shape our own lives.

    Ultimately, the particular great thing regarding individualized photograph collages is their capability to preserve recollections in a deeply meaningful, creative way. They transform personal snapshots into some sort of cohesive work of art, taking the essence associated with life’s most unforgettable moments.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。