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’ll Be Unable To Guess Private Psychiatrist Sheffield Cost’s Secrets Private Psychiatrist sheffield
What’s The Current Job Market For Situs Gotogel Terpercaya Professionals Like?
situs gotogel terpercaya – 0522565551.ussoft.kr,
You’ll Never Be Able To Figure Out This Situs Alternatif Gotogel’s
Secrets Situs Alternatif Gotogel (https://Morozmarket.Ru)
The Little-Known Benefits Of Lost My Car Keys No Spare lost My Car keys what do i
do (dudoser.com)
For Whom Is Lost Auto Key Replacement And Why You Should
Be Concerned lost car key replacement
I’m really loving the theme/design of your blog. Do you ever run into any browser
compatibility issues? A couple of my blog visitors have complained
about my blog not working correctly in Explorer but looks great in Opera.
Do you have any advice to help fix this problem?
Hello to every body, it’s my first visit of this blog;
this blog includes awesome and actually good data in support of readers.
A Positive Rant Concerning Buy Mobility Scooters best mobility
scooter for steep hills uk (Adell)
Guide To Double Glazing Window Repairs: The Intermediate Guide To Double Glazing Window Repairs Window repair
Why Everyone Is Talking About Key Programming Right Now Car Keys Cutting And Programming
What i don’t realize is in fact how you are
no longer actually much more neatly-liked than you might be right now.
You are very intelligent. You recognize therefore significantly in the case of this subject, made me personally believe it from so
many various angles. Its like men and women don’t seem to be fascinated until it is
one thing to accomplish with Woman gaga! Your individual stuffs nice.
All the time maintain it up!
See What Dreametech L30 Ultra Tricks The Celebs Are Using Dreametech l30
Why Is There All This Fuss About Avon Ultimate Welcome Kit?
Avon Ultimate Welcome Kit
Why No One Cares About Dreame Bot L30 Ultra Dreame Ultra l30
Undeniable Proof That You Need Used Mobility Scooters Near Me 4 wheel mobility scooter near me
If you are going for best contents like myself, just
visit this site every day as it gives feature contents, thanks
Say “Yes” To These 5 Key Mercedes Tips Mercedes Key
What Experts In The Field Of Online Clothing Sites Uk Want You To Know?
100 sheet target Pack
The Companies That Are The Least Well-Known To Follow In The Avon Planet Spa Industry avon planet spa sleep
(m.042-527-9574.1004114.co.kr)
The Next Big Trend In The Mesothelioma Legal Question Industry mesothelioma claim
There Is No Doubt That You Require Double Glazing Near Me double hlazing
I savor, lead to I found just what I used to be taking a look for.
You have ended my 4 day long hunt! God Bless you man. Have a nice
day. Bye
Seven Explanations On Why Double Glazing Repair Leeds Is So Important double glazed Window repairs
В этом что-то есть. Спасибо за помощь в этом вопросе. Все гениальное просто.
Оформить документ об образовании, если вы не завершили 9 классов, можно, воспользовавшись услугами специализированных организаций, которые предлагают такого рода услуги. Одним из наиболее надежных способов является Купить аттестат за 9 классов http://diplomsagroups.com/kupit-attestat-shkoly/za-9-klass.html через сеть, что позволяет экономить ваше время и получить необходимый документ без необходимости посещения учебных заведений.
Awesome article.
The Reasons Why Adding A Tommy Hilfiger Bag Handbag To Your Life’s
Journey Will Make The The Difference tommy hilfiger handbag, Isiah,
This is a topic which is near to my heart… Many thanks!
Where are your contact details though?
Wow, awesome weblog structure! How long have
you been blogging for? you made blogging glance easy.
The overall glance of your web site is great, let alone the content material!
The 10 Most Terrifying Things About Double Glazing
Company Near Me double glazing company near me (Randall)
Why Everyone Is Talking About Double Glazing Offers Near Me Right
Now wooden Double glazed windows