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
The 10 Most Terrifying Things About Upvc
Door Repairs Near Me upvc Door repairs near Me
I love it whenever people get together and share ideas.
Great blog, stick with it!
7 Small Changes That Will Make The Biggest Difference In Your ADHD
Diagnosis Adults Private UK Private Assessment For
Adhd Northern Ireland (http://Aragaon.Net/Bbs/Board.Php?Bo_Table=Review&Wr_Id=412756)
Medication information sheet. Cautions.
how to buy levonorgestrel without rx
Everything about pills. Get information here.
See What Psychiatric Assessment Form Tricks The Celebs Are Making
Use Of psychiatric assessment form (Marietta)
The Next Big Thing In The Double Glazed Units Near Me Industry Replacement Double Glazed Units Near Me
Fantastic stuff, Regards.
Business Card Printing For Graphic Design Firms
200만원 대출
10 Facts About Upvc Window Repair Near Me That Can Instantly Put You In A Good Mood upvc window repairs near me
Приветствую. Подскажите, где найти разные статьи о недвижимости? Сейчас читаю https://vortex-los.ru
Very nice post. I just stumbled upon your blog and wished
to say that I have really enjoyed browsing your blog posts.
In any case I’ll be subscribing on your rss feed and I’m hoping you write again very
soon!
The Secret Life Of Mental Health Assessment initial mental health assessment;
Jada,
Hi there! pharmacy tech online great website.
Nice post. I was checking constantly this weblog and I’m inspired!
Extremely helpful info particularly the last section 🙂 I handle such information much.
I used to be seeking this certain information for a long time.
Thanks and best of luck.
With thanks! Ample material.
Marvelous, what a webpage it is! This webpage provides helpful information to us, keep
it up.
You’ll Never Guess This Double Glaze Repair Near Me’s Tricks
Double glaze repair near me
Отличный сатй! Всем рекомендую!скупка золота москва
Pills prescribing information. Generic Name.
can i order generic pulmicort for sale
All what you want to know about medicines. Read information here.
Three Reasons Why The Reasons For Your Boot Scooters Is Broken (And
How To Fix It) arlennizo
The Most Hilarious Complaints We’ve Seen About Adhd
In Women Symptoms adhd Symptoms in Adults uk
A motivating discussion is definitely worth comment.
I think that you ought to publish more on this issue, it
might not be a taboo matter but typically people don’t
discuss such issues. To the next! Cheers!!
https://atomicketo.net have been gaining popularity in the health and wellness community for their potential benefits in aiding weight loss, improving digestion, and boosting energy levels.
Pretty nice post. I just stumbled upon your weblog and wished to say
that I have truly enjoyed surfing around your blog posts.
After all I’ll be subscribing to your feed and
I hope you write again soon!
http://www.daecheonkim.kr/bbs/board.php?bo_table=free&wr_id=163658
I think this is among the most vital information for me.
And i am glad reading your article. But should remark on few general things, The
wensite style is ideal, the articles is really
nice : D. Good job, cheers
Medicine information for patients. Drug Class.
where to get cheap minocycline
Best what you want to know about drug. Read now.
Neat blog! Is your theme custom made or did you download it from somewhere?
A theme like yours with a few simple adjustements would
really make my blog jump out. Please let me know where you got your
design. Many thanks
How To Recognize The Upvc Repairs Near Me That’s Right For You
Window repair Near me
Five Lessons You Can Learn From Window Repair Near Upvc Window Repair Near Me
Medicines information for patients. Drug Class.
can i buy generic bactrim
Everything information about medicines. Get information here.