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
Five Killer Quora Answers To Upvc Window Repairs Upvc Window Repairs
The 10 Most Scariest Things About Upvc Door Repairs Near Me upvc door repairs near me
What Is Mazda 2 Key Replacement And Why Is Everyone
Talking About It? mazda key fob; Renaldo,
The Ultimate Guide To Double Glazed Window Repairs Near Me Replacing double glazing glass
Kamagra 100mg price: buy kamagra online – sildenafil oral jelly 100mg kamagra
To apply for small payday loans on-line with well-reputed, trustworthy lenders, Viva Payday Loans is a loan matching service that’s getting consideration.
I was curious if you ever considered changing the layout of your website?
Its very well written; I love what youve got to say. But maybe you could a little more
in the way of content so people could connect with it better.
Youve got an awful lot of text for only having one or two images.
Maybe you could space it out better?
Howdy! This post couldn’t be written much better! Reading through this post reminds me of my
previous roommate! He continually kept preaching about
this. I most certainly will send this post to him.
Pretty sure he’ll have a very good read. Thank you for sharing!
Thanks for sharing such a pleasant idea, post is fastidious,
thats why i have read it fully
Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Loved it!
buy Levitra over the counter: Levitra generic price – Vardenafil buy online
http://levitrav.store/# Vardenafil online prescription
Hi would you mind stating which blog platform you’re working with?
I’m looking to start my own blog in the near future but I’m having a tough time selecting 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 Sorry for being off-topic but I had to ask!
Feel free to visit my homepage: Puff Wow
What’s The Job Market For Which Is The Best Online Supermarket Professionals?
which is the best online supermarket (http://www.climberf1.com)
This article gives clear idea designed for the new viewers of blogging, that in fact how to do blogging.
https://cialist.pro/# Cheap Cialis
The Little-Known Benefits Of Lightweight Folding
Travel Mobility Scooters foldable Mobility scooter for Adults
http://cenforce.pro/# cenforce.pro
Hello there! I know this is kinda off topic nevertheless I’d figured I’d ask.
Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa?
My site discusses a lot of the same topics as yours and I believe we
could greatly benefit from each other. If you might be interested feel free
to send me an e-mail. I look forward to hearing from you!
Superb blog by the way!
An All-Inclusive List Of Replacement Key For Nissan Micra Dos And Don’ts replace nissan key
fob (https://Www.cheaperseeker.com/u/pinfarm46)
Purchase Cenforce Online: Purchase Cenforce Online – order cenforce
11 Creative Methods To Write About Adult Adhd Medication adhd Medications uk
buy Levitra over the counter: levitrav.store – Vardenafil buy online
The 10 Most Worst Windows Leeds Fails Of All
Time Could Have Been Prevented window replacement leeds
http://kamagra.win/# Kamagra tablets
Excellent blog! Do you have any suggestions for aspiring writers?
I’m hoping to start my own website soon but I’m a little lost on everything.
Would you suggest starting with a free platform
like WordPress or go for a paid option? There are so many options out there that I’m
totally overwhelmed .. Any ideas? Thanks a lot!
Cheap Sildenafil 100mg Cheap Viagra 100mg buy viagra here
https://kamagra.win/# buy Kamagra
azithromycin buy online india
https://levitrav.store/# Cheap Levitra online