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
What Is Akun Demo Slot And Why Is Everyone Talking About It?
akun demo maxwin, Georgianna,
Why Everyone Is Talking About Situs Gotogel Terpercaya Right Now Bandar Judi
Everything is very open with a clear description of the issues.
It was truly informative. Your site is useful.
Thank you for sharing!
https://cialist.pro/# п»їcialis generic
15 Shocking Facts About Prada Handbag Man That You Didn’t Know prada handbags mini –
http://yerliakor.com/User/Susancopy17 –
Buy Levitra 20mg online: Vardenafil online prescription – Vardenafil buy online
From All Over The Web From The Web: 20 Awesome Infographics About Citroen C3 Key Fob Replacement replacement key For Citroen c1
http://viagras.online/# Cheap Viagra 100mg
Volkswagen Keys Replacement 10 Things I’d Like To Have Known Earlier volkswagen Passat key fob
10 Things That Your Family Teach You About Audi Keys Audi
Connect Key (M1Bar.Com)
9 Things Your Parents Taught You About Designer Handbags For Ladies designer Handbags
Buy Cenforce 100mg Online: cheapest cenforce – order cenforce
I am really enjoying the theme/design of your blog.
Do you ever run into any browser compatibility issues?
A handful of my blog readers have complained about my
site not working correctly in Explorer but looks great in Opera.
Do you have any recommendations to help fix this issue?
11 “Faux Pas” That Are Actually Okay To Create Using
Your Sbobet goblok
Cialis 20mg price in USA Cialis 20mg price in USA Cialis without a doctor prescription
A Look At The Good And Bad About Top Table Freezer Small Freezers
cheap kamagra kamagra pills sildenafil oral jelly 100mg kamagra
{هورشیدلند فروشگاه تخصصی در
زمینه هدیه سازمانی و هدیه تبلیغاتی
با تنوع بینظیر، کیفیت بالا
و قیمت مناسب}{هدیه تبلیغاتی بهترین راه برای انجام
تبلیغات و رساندن پیامهای شرکت به
مخاطبان و همچنین معرفی برند شما }.مجموعه
هورشیدلند بهترین در زمینه طراحی و تولید هدیه های اختصاصی برای سازمان ها میباشد.{هدیه
تبلیغاتی و سازمانی خاص و ماندگار با هورشیدلند }{پک های هدیه سازمانی با تنوع بالا و قیمت مناسب بهترین انتخاب برای خرید
هدیه در مناسبت های مهم.}{تولید انواع هدیه تبلیغاتی خاص و با کیفیت و
به صرفه در فروشگاه اینترنتی هورشیدلند}
https://cialist.pro/# Tadalafil price
https://cialist.pro/# Cheap Cialis
Meds information for patients. Cautions.
prozac order
Everything trends of pills. Read now.
Howdy this is somewhat of off topic but I was wanting to know
if blogs use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding experience so I wanted to get advice from someone
with experience. Any help would be greatly appreciated!
http://cialist.pro/# Cialis over the counter
You’ll Never Guess This Window Replacement
Near Me’s Benefits Window replacement near me
Purchase Cenforce Online: Purchase Cenforce Online – Buy Cenforce 100mg Online
10 Double Glazing Repair Near Me That Are Unexpected double glazing Repairs
naturally like your website but you need to test the spelling on quite
a few of your posts. Many of them are rife with spelling issues and I to find it very bothersome to inform
the truth nevertheless I’ll surely come back again.
Great blog! Is your theme custom made or did you download
it from somewhere? A design like yours with a
few simple adjustements would really make my blog
stand out. Please let me know where you got your theme.
Kudos
Kamagra tablets: kamagra.win – super kamagra
I couldn’t refrain from commenting. Very well written!