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
10 Things Everyone Has To Say About CSGO Most Profitable Cases
CSGO Most Profitable Cases operation wildfire case (http://Www.dermandar.com)
Medicament information for patients. Short-Term Effects.
how to buy generic ipratropium without rx
All information about drugs. Get information now.
Drugs prescribing information. Generic Name.
can you buy spiriva price
Everything news about medication. Read information now.
5 Killer Quora Answers To Replacement Double Glazing
Units Near Me Double glazing Units Near Me
9 . What Your Parents Teach You About Car Key Replacement Service
Near Me Car Key Replacement Service Near Me (http://Www.Yonginmarathon.Com)
I constantly spent my half an hour to read this blog’s content everyday
along with a mug of coffee.
Livetotobet adalah situs news berita olahraga sepakbola terupdate dengan menyajikan fakta seputar dunia olahraga sepakbola semoga informasi yang diberikan oleh live totobet dapat
bermanfaat untuk penggemar berita sepakbola dunia
Do You Think You’re Suited For Double Glazing Company Near Me?
Try This Quiz windows double glazing
What’s The Most Common Mazda Key Debate Could Be As Black And White As You Might Think Mazda Cx 5 Keys Locked In Car
I quite like reading through a post that will make people think.
Also, thank you for permitting me to comment!
This piece of writing will assist the internet
people for setting up new webpage or even a weblog from start to end.
Say “Yes” To These 5 Upvc Door Panels Tips Upvc Door Panels Replacements
What’s The Job Market For Double Glazed Repairs Near Me Professionals Like?
double glazed repairs near me (Marcel)
A Step-By Step Guide To Railroad Injuries Law railroad Injuries Law firm
I discovered more a new challenge on this weight loss issue. A single issue is a good nutrition is tremendously vital while dieting. A tremendous reduction in bad foods, sugary food items, fried foods, sugary foods, red meat, and white flour products could be necessary. Retaining wastes parasites, and poisons may prevent goals for fat-loss. While particular drugs briefly solve the challenge, the awful side effects aren’t worth it, and in addition they never give more than a non permanent solution. This is a known proven fact that 95 of celebrity diets fail. Thanks for sharing your thinking on this web site.
Ten Pinterest Accounts To Follow About Slot Updates low variance slots
Overwhelmed by how to start the recovery of your QIWI wallet funds?
Our customer support is on standby around the clock to guide you through the procedure and answer
any questions you may encounter.
With us, you’re opting for a reliable helper in getting your money back.
How To Determine If You’re Ready For Slot Developers Slot games
12 Companies That Are Leading The Way In Door Repair Near Me Window Replacement Near Me
Unexpected Business Strategies For Business That Aided
Truck Accident Attorney Near Me Succeed law
What’s The Job Market For Popular Slots Professionals? Popular slots (muabanthuenha.com)
The 9 Things Your Parents Taught You About Unique Slots unique Slots
5 Laws That Will Help Industry Leaders In Slot Games Industry
top developer slots
But strangers’ concerns and judgmental opinions reflect more on our outdated notions about pregnancy and the stigma against sex work than they do on Summer’s choice.Sebastian,ラブドール えろ
You’ll Be Unable To Guess Top Slots’s Tricks top slots (gdchuanxin.com)
Hi there! This is my first visit to your blog!
We are a team of volunteers and starting a new initiative in a community in the same niche.
Your blog provided us valuable information to work on. You have done a
wonderful job!
The 9 Things Your Parents Teach You About Best Online Clothing Sites Uk best Online clothing sites uk
What Is Can I Buy From A Uk Website And Why Is Everyone Dissing It?
Long-Lasting Humidifier Filters
A Guide To Themed Slots From Start To Finish entertaining slots (http://120.zsluoping.cn/home.php?mod=space&uid=571437)
Five Things Everyone Makes Up On The Subject Of Window Repairman emergency