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,425,284 Responses

  1. BrandonSaisy表示:

    Online medicine home delivery: Order medicine from India to USA – top 10 online pharmacy in india

  2. Jorgeagopy表示:

    drugs online: cheap drugs online – medication drugs

  3. patio Gas表示:

    Patio Heater Gas Cylinder: The Ugly Real Truth Of Patio Heater
    Gas Cylinder patio Gas

  4. BrandonSaisy表示:

    buy medicines online in india: Indian pharmacy international shipping – mail order pharmacy india

  5. LeroyBak表示:

    best online drugstore cheaper medications prescription meds without the prescriptions

  6. DevonPhack表示:

    https://pharm24.pro/# canadian online pharmacy
    canadian pharmacy

  7. Diplomi_lyoi表示:

    купить диплом в тагиле prema-diploms.ru .

  8. AbecPaish表示:

    Manufacturers offer low price of target pharmacy propecia when they are buying it online.

  9. anime porn表示:

    I think what you posted made a great deal of sense. But, what about this?
    suppose you added a little content? I ain’t suggesting your content is not solid, but suppose you
    added something that makes people want more? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室 is a
    little boring. You should look at Yahoo’s
    front page and watch how they create news headlines to get people interested.
    You might add a video or a related pic or two to get readers interested about
    everything’ve got to say. Just my opinion, it would make your website
    a little bit more interesting.

  10. Spot on with this write-up, I honestly believe this web site needs
    a lot more attention. I’ll probably be back again to read more, thanks for the info!

  11. cheap表示:

    12 Stats About Audi Spare Key To Make You Look Smart Around Other People cheap

  12. Bokepindoh表示:

    I simply couldn’t depart your site before suggesting that I really loved the standard info a person provide on your
    guests? Is going to be back regularly to check up on new posts

  13. LeroyBak表示:

    purple pharmacy mexico price list mexico pharmacy cheap mexican online pharmacies prescription drugs

  14. Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Liked it!

  15. At this time I am going to do my breakfast, once having my breakfast coming yet again to read more news.

    https://w1.resultnomor.us/

  16. Its like you read my mind! You appear to know so
    much about this, like you wrote the book in it or something.
    I think that you can do with some pics to drive the message home a little bit, but other than that, this is excellent blog.
    A great read. I’ll definitely be back.

  17. We are a group of volunteers and starting a new scheme in our community.
    Your website offered us with valuable info to work on. You have done a formidable job and our entire community will be thankful
    to you.

  18. Medication prescribing information. Cautions.
    can i order cheap benicar
    All news about medicines. Get now.

  19. BrandonSaisy表示:

    indian pharmacies safe: indian pharm 24 – indian pharmacy

  20. LeroyBak表示:

    pharmacy website india Indian pharmacy to USA online shopping pharmacy india

  21. It’s really a nice and helpful piece of information. I’m happy that you
    just shared this helpful info with us. Please stay us up to
    date like this. Thank you for sharing.

  22. LeroyBak表示:

    world pharmacy india Indian pharmacy to USA reputable indian online pharmacy

  23. LeroyBak表示:

    mexican pharmaceuticals online mexican drugs purple pharmacy mexico price list

  24. dubstep表示:

    When someone writes an paragraph he/she keeps the image of a
    user in his/her mind that how a user can understand
    it. Thus that’s why this paragraph is great. Thanks!

    Also visit my blog post :: dubstep

  25. 20 Trailblazers Leading The Way In Wall Fireplace Electric Electric Firesuite

  26. Citroen Berlingo Van Key Replacement Isn’t As Tough As You Think
    replacement citroen c1 key nottingham – http://www.stes.Tyc.edu.tw/xoops/modules/profile/userinfo.php?uid=1802269

發佈留言

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