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,266,263 Responses

  1. DarioTob表示:

    пин ап зеркало: пин ап казино – pinup-kazi.ru

  2. Nine Things That Your Parent Taught You About Key Fob Repair Service key fob repair service

  3. DarioTob表示:

    пинап казино: пин ап казино официальный сайт – пин ап казино официальный сайт

  4. Jamesdiure表示:

    вавада онлайн казино: вавада казино зеркало – вавада казино

  5. I never realized how easy it is to fix minor issues myself until I read some guides online phone repair

  6. Everything is very open with a clear explanation of the issues.

    It was truly informative. Your website is very useful.
    Thank you for sharing!

  7. DarioTob表示:

    пин ап казино: pinup – pinup kazi

  8. Iyannaobesk表示:

    jackpoker играть онлайн
    jackpoker играть бесплатно

  9. I don’t know if it’s just me or if everybody else encountering problems with your
    site. It appears as if some of the written text within your
    content are running off the screen. Can someone else
    please provide feedback and let me know if this
    is happening to them as well? This could be a problem with my web browser because I’ve had this
    happen previously. Cheers

  10. Jamesdiure表示:

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

  11. Bracelets usually snap on the wrist with a hook or clasp.

  12. Josephmax表示:

    https://pinup-kazi.ru/# пин ап казино официальный сайт

  13. hello!,I really like your writing very so much! proportion we keep up a correspondence more about your article on AOL?
    I need an expert on this house to solve my problem.
    May be that is you! Taking a look ahead to see you.

  14. Leonard Owen表示:

    A proactive technique to security can prevent losses that far surpass the expense of employing guards TreeStone Security Services

  15. Unquestionably imagine that that you said. Your favourite reason seemed to be at the web the easiest factor to take into accout of.

    I say to you, I certainly get irked while other folks consider concerns
    that they plainly don’t know about. You controlled to hit the nail upon the top and also defined out the whole thing with no need side-effects , folks can take
    a signal. Will likely be back to get more.
    Thank you

  16. Nikefut表示:

    darknet drug links dark website darknet sites

  17. LeonardKnism表示:

    кухни на заказ от производителя – Эксклюзивные проекты кухонь по доступным ценам.

  18. Medicines information for patients. Cautions.
    trazodone 50 mg directions
    Actual about medicament. Read here.

  19. CurtisIncix表示:

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

  20. DarioTob表示:

    вавада онлайн казино: vavada-kazi.ru – vavada-kazi.ru

  21. The Prisoner in the American Revolution, p.

  22. Josephmax表示:

    http://pinup-kazi.ru/# pinup-kazi.ru

  23. Vera Lambert表示:

    Has anyone else turned their garage into a living space? I got fantastic ideas from Revive Renovations bathroom remodeling that made it all

  24. DarioTob表示:

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

發佈留言

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