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,479,117 Responses

  1. Markdob表示:

    Всем привет! Подскажите, где почитатьразные статьи о недвижимости? Сейчас читаю – https://zapchasti-qtz.ru

  2. Markdob表示:

    Всем привет! Может кто знает, где почитатьразные блоги о недвижимости? Сейчас читаю – https://zapchasti-qtz.ru

  3. Cody Nichols表示:

    Video marketing is definitely on the rise! It’s exciting to see how brands are using it creatively. Explore more ideas at SEO Company Bristol

  4. Markdob表示:

    Всем привет! Может кто знает, где почитатьразные статьи о недвижимости? Сейчас читаю – https://zapchasti-qtz.ru

  5. I feel that is among the so much vital information for me.
    And i am satisfied reading your article. However want to commentary on few general things, The site taste is wonderful,
    the articles is really great : D. Excellent task,
    cheers

  6. ngewe表示:

    Hey would you mind stating which blog platform you’re working with?
    I’m planning to start my own blog soon but I’m having a difficult time
    choosing between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your design and style seems different then most
    blogs and I’m looking for something completely unique.
    P.S Apologies for getting off-topic but I had to ask!

  7. лечение наркозависимости в стационаре klin.0pk.me/viewtopic.php?id=4409 .

  8. The latest industry insights and research underscore this development.irontech doll Many high-end sex dolls now feature incredibly realistic details like authentic skin textures and precise anatomical designs.

  9. Markdob表示:

    Приветствую. Может кто знает, где почитатьразные статьи о недвижимости? Пока нашел – https://zapchasti-qtz.ru

  10. kontol表示:

    At this time it looks like Expression Engine is the top blogging platform out there right now.
    (from what I’ve read) Is that what you’re using on your blog?

  11. Cornelius表示:

    Have you ever considered about including a little bit more than just your articles?
    I mean, what you say is important and all. However think of if you
    added some great photos or videos to give your posts more,
    “pop”! Your content is excellent but with images and video clips, this site could definitely be one of
    the most beneficial in its field. Amazing blog!

  12. Markdob表示:

    Приветствую. Подскажите, где найтиполезные статьи о недвижимости? Пока нашел – https://zapchasti-qtz.ru

  13. Viagra pills表示:

    This is a very good tip especially to those fresh
    to the blogosphere. Simple but very accurate info… Thanks for sharing this
    one. A must read post!

  14. Awesome! Its Realtor in Meridian Mississippi fact amazing piece of writing, I have
    got much clear idea on the topic of from this piece of writing.

  15. Markdob表示:

    Приветствую. Подскажите, где почитатьполезные блоги о недвижимости? Пока нашел – https://zapchasti-qtz.ru

  16. Hey I am so thrilled I found your site, I really found you by error,
    while I was looking on Askjeeve for something else,
    Nonetheless I am here now and would just like to say many thanks
    for a remarkable post and a all round thrilling blog (I also love the theme/design),
    I don’t have time to read through it all at the moment but I have bookmarked it and also added realtor in ponce inlet fl your RSS feeds, so when I have time I will be back to read much more, Please do keep up
    the superb b.

發佈留言

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