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
15 Shocking Facts About Semi Truck Settlement You’ve Never Known Semi truck Accident
14 Smart Ways To Spend Your Leftover Motorcycle Accident Compensation Budget lawyers
Medicine information sheet. Long-Term Effects.
can you get kamagra for sale
Everything trends of medicine. Get here.
Howdy! how to get finasteride prices excellent website.
Hi, ater reading this remakable piece of writing і аm also glad to share my know-how
here with colleagues.
The Top Reasons Why People Succeed In The Double Glazing Near
Me Industry double glazing company near me (Gerald)
Right here is the right webpage for anybody who really wants to understand this topic.
You know a whole lot its almost hard to argue with you
(not that I personally would want to…HaHa). You definitely put a brand new spin on a
subject that’s been written about for a long time. Excellent stuff,
just excellent!
Drugs information for patients. Drug Class.
cost of generic zoloft online
Some trends of medicines. Read here.
See What Online Shopping Figures Uk Tricks The Celebs Are Utilizing online Shopping figures uk
When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time
a comment is added I get four emails with the same comment.
Is there any way you can remove people from that service? Many thanks!
You’ll Never Be Able To Figure Out This ‘s Benefits upvc Door repairs near me
Hmm it appears like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
I too am an aspiring blog blogger but I’m still new to the
whole thing. Do you have any points for newbie blog writers?
I’d definitely appreciate it.
order generic advair diskus tablets
A Brief History Of Upvc Doors In 10 Milestones replacement
door panel upvc [Berenice]
The Companies That Are The Least Well-Known To Follow
In The Boat Accident Attorney Industry boat Accident lawyer
What’s The Job Market For Upvc Repairs Near Me Professionals Like?
Upvc Repairs Near Me
jasminer
I like the valuable information you provide for your articles.
I’ll bookmark your blog and test once more right here regularly.
I’m quite sure I will be informed many new stuff proper here!
Best of luck for the next!
Discovering a language learning apps reddit does not have to be expensive.
Всем привет! Подскажите, где почитать полезные статьи о недвижимости? Пока нашел https://1eve1.ru
tretinoin 0.25 cream price
trazodone 100mg sale
20 Trailblazers Leading The Way In Door Fitting Luton window and doors company near me
The 10 Scariest Things About Online Shopping Sites London online shop
Seasonal allergic rhinitis exacerbation support services
I was thoroughly impressed by the depth of analysis in your article on [specific topic].ラブドールYou’ve covered all the critical angles and provided a well-rounded view that is both comprehensive and easy to understand.
Guide To Upvc Windows Near Me: The Intermediate Guide For Upvc Windows Near Me upvc windows near me (Celia)
Ten Things You Shouldn’t Post On Twitter attorney
The Most Hilarious Complaints We’ve Seen About Upvc Window Repairs
window repairs near me (Maybell)
This Is How Auto Accident Settlement Will Look In 10 Years auto accident Attorney