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,195,241 Responses

  1. How To Know If You’re Ready To Go After Veterans Disability Lawsuit veterans disability law Firm

  2. oscarreys.top表示:

    20 Trailblazers Lead The Way In Hades Gods Tier List
    oscarreys.top

  3. hacklink表示:

    homosexual porn

  4. MichaelPayof表示:

    mexican drugstore online
    https://cmqpharma.online/# medicine in mexico pharmacies
    mexican drugstore online

  5. amarms.com表示:

    Building Backlinks For Could 구글지니어스 [amarms.com]

  6. MichaelPayof表示:

    medication from mexico pharmacy
    http://cmqpharma.com/# п»їbest mexican online pharmacies
    mexican pharmaceuticals online

  7. Antoniowap表示:

    Hi there! erection pills online beneficial website.

  8. MichaelPayof表示:

    buying prescription drugs in mexico online
    https://cmqpharma.online/# buying from online mexican pharmacy
    buying prescription drugs in mexico

  9. MichaelPayof表示:

    mexican border pharmacies shipping to usa
    https://cmqpharma.com/# buying prescription drugs in mexico
    purple pharmacy mexico price list

  10. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://amaterasuspa.pl/qrque of type and scrambled it to make a type specimen book.

  11. You really make it appear really easy together with your presentation but I to find this topic to be
    actually something which I think I might never
    understand. It sort of feels too complex and very
    wide for me. I am looking ahead to your subsequent publish, I’ll attempt to get the hang of it!

  12. 5611432.xyz表示:

    15 Best Pinterest Boards Of All Time About Car Key Fob Programming Near
    Me 5611432.xyz

  13. MichaelPayof表示:

    mexican rx online
    https://cmqpharma.online/# pharmacies in mexico that ship to usa
    mexico drug stores pharmacies

  14. I think what you typed was very logical. However, think about this, suppose you added a little information?
    I ain’t saying your content is not good, but suppose you added
    a post title that grabbed people’s attention? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室 is
    a little plain. You ought to look at Yahoo’s home page and see how they write post titles to
    get people to click. You might try adding a video or a related picture or two to grab people interested about everything’ve got to say.
    In my opinion, it could bring your blog a little livelier.

  15. MichaelPayof表示:

    mexican mail order pharmacies
    http://cmqpharma.com/# pharmacies in mexico that ship to usa
    mexico drug stores pharmacies

  16. sex表示:

    I do not even know how I ended up here, but I thought this post was great.
    I don’t know who you are but certainly you’re going to a famous blogger if
    you are not already 😉 Cheers!

  17. MichaelPayof表示:

    mexican border pharmacies shipping to usa
    http://cmqpharma.com/# mexico drug stores pharmacies
    mexican rx online

  18. MichaelPayof表示:

    mexico pharmacies prescription drugs
    http://cmqpharma.com/# mexico drug stores pharmacies
    medication from mexico pharmacy

  19. MichaelPayof表示:

    mexican mail order pharmacies
    http://cmqpharma.com/# mexican rx online
    purple pharmacy mexico price list

  20. You’ll Never Be Able To Figure Out This Window Replacement Near Me’s Tricks Window Replacement Near Me

  21. MichaelPayof表示:

    medicine in mexico pharmacies
    http://cmqpharma.com/# mexican drugstore online
    buying from online mexican pharmacy

  22. Watch Out: How Wall Fireplace Electric Is Taking Over And What
    To Do About It lynnbolvin.top

  23. I could not resist commenting. Exceptionally well written!

  24. I like reading a post that will make people think.
    Also, thanks for permitting me to comment!

發佈留言

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