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,265,706 Responses

  1. Wow, this post is fastidious, my younger sister is analyzing such things,
    thus I am going to inform her.

  2. 1с бухгалтерия 8.3 базовая https://www.1s-buhgalteriya-kupit.ru .

  3. What’s up to all, it’s in fact a good for me to pay a quick visit this
    web page, it includes priceless Information.

  4. вывод из запоя на дому похмельная https://www.honey.ukrbb.net/viewtopic.php?f=45&t=16709 .

  5. 1с бухгалтерия 8.3 базовая 1s-buhgalteriya-kupit.ru .

  6. DarioTob表示:

    pinup: pinup-kazi.kz – пин ап казино онлайн

  7. 光とは表示:

    Waite, Thomas L. (April 24, 1988).

  8. nodeposit表示:

    My programmer is trying to convince me to move to .net from
    PHP. I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using WordPress on a number of websites for about
    a year and am concerned about switching to another platform.
    I have heard fantastic things about blogengine.net.
    Is there a way I can import all my wordpress posts into it?

    Any kind of help would be greatly appreciated!

  9. HenrydeT表示:

    pinup-kazi.ru пин ап казино pinup kazi

  10. Nikefut表示:

    darknet drugs dark website drug markets dark web

  11. MLB表示:

    Hello There. I found your blog using msn. This is an extremely well written article.
    I will make sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I’ll certainly return.

  12. Roy Phelps表示:

    What’s your take on utilizing dark colors in small spaces? There are wonderful layout ideas offered at carpet installation

  13. Josephmax表示:

    https://vavada-kazi.ru/# вавада

  14. paypaldan türkiyeye para gönderme

    you’re actually a just right webmaster. The website loading pace is amazing.

    It sort of feels that you’re doing any distinctive trick.
    In addition, The contents are masterwork. you’ve performed a great task on this subject!

  15. Jamesdiure表示:

    пин ап казино онлайн: пин ап казино – pin up казино

  16. DarioTob表示:

    pin up казино: пин ап кз – pin up казино

  17. Medicament information. What side effects?
    how to get cheap zithromax pills
    All about medicine. Read information here.

發佈留言

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