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 Healthy Leather Couch Set Habits Leather Sofa Set
The Best Pragmatic Techniques To Make A Difference In Your Life 프라그마틱 무료게임
Greetings! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy
reading through your posts. Can you suggest any other blogs/websites/forums that go over the same subjects?
Thanks!
Who Is Upvc Door Panels And Why You Should Care upvc Door panels near me (https://botdb.win)
I think this is one of the most vital information for me.
And i’m glad reading your article. But wanna remark on few general things, The website
style is wonderful, the articles is really excellent :
D. Good job, cheers
В магазине сейфов предлагают купить сейф в москве сейфы
https://ozempic.art/# ozempic cost
Are You Responsible For An Composite Door Replacement Budget?
12 Ways To Spend Your Money Composite Door Replacement Parts
https://rybelsus.shop/# buy semaglutide online
В магазине сейфов предлагают купить сейф стоимость сейфа
A Comprehensive Guide To ADHD Private Assessment.
Ultimate Guide To ADHD Private Assessment Private adult adhd assessment near Me
30 Inspirational Quotes About Kids Bunk Bed Best Kids Bunk Bed
Hi there all, here every person is sharing these experience,
thus it’s pleasant to read this webpage, and I used to pay a
visit this web site everyday.
В магазине сейфов предлагают сейф купить цена сейф купить в москве
I have read so many content concerning the blogger lovers
but this article is truly a pleasant post, keep
it up.
Why We Enjoy Situs Togel Dan Slot Terpercaya (And You Should,
Too!) Situs Togel Terpercaya
How To Solve Issues Related To ADHD Medication Ritalin doctors Prescribing adhd Medication
Teгrific article! Thaat is the type of info that arrе supposed to
be shaeed around the internet. Disgrace оn the searⅽh
engines for now not positіoning this post higher! Ꮯome on over and discuѕs with my website .
Thank you =)
Leather Sofa Set Tips To Relax Your Everyday Lifethe Only Leather Sofa Set Trick That Every Person Should Be Able To leather sofa set, Vernon,
How Much Can Best Coffee Machine Experts Earn? best Coffee machine bean to cup
rybelsus coupon buy semaglutide pills rybelsus price
How To Recognize The Private Psychiatrist Belfast Cost Which Is Right For You private psychiatrist derry (Anne)
The Next Big Thing In The Lamborghini Centenario Key Industry lamborghini Countach Key (humphrey-Hein-3.blogbright.net)
https://ozempic.art/# buy cheap ozempic
В магазине сейфов предлагают сейф цена купить купить сейф в москве цена
rybelsus price: semaglutide tablets – semaglutide cost
15 Reasons You Shouldn’t Overlook Link Togel Resmi bandar toto
One Key Trick Everybody Should Know The One Mouth Fleshlight Trick Every Person Should Know Porn Star Fleshlights
Guide To Situs 4d: The Intermediate Guide In Situs 4d situs 4D
Excellent post however , I was wanting to know if you could write a litte more
on this subject? I’d be very grateful if
you could elaborate a little bit more. Bless you!
Here is my page – Mostbet Casino