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
Need Inspiration? Try Looking Up Window Repair Near Upvc Window Repair Near Me
5 Slot Tournaments Projects For Any Budget evoplay slots bonus Rounds
Very soon this site will be famous amid all blog people, due to it’s nice content
my blog post – ฟัน88
Is Technology Making Land Rover Replacement Key Better Or Worse?
land rover key reprogramming [Hulda]
I always used to study piece of writing in news papers but now as I
am a user of net so from now I am using net for articles or reviews, thanks to web.
What You Should Be Focusing On Improving Slot Reviews high RTP slots (https://www.google.com.sb/)
Hey! I just wanted to ask if you ever have any trouble with hackers?
My last blog (wordpress) was hacked and I ended up
losing several weeks of hard work due to no
backup. Do you have any methods to stop hackers?
10 Top Mobile Apps For Certified Slots trusted Slots
Эти же способы рекомендуется использовать для мгновенного вывода с казино.
continuously i used to read smaller posts which as well clear
their motive, and that is also happening with this post which I
am reading here.
Seat Car Key Cover Tools To Make Your Daily Life
seat key replacement (Veda)
לעוד ועוד. אז היכן מוצאים את נערות הליווי, מה היתרונות שלהן והיכן ניתן לבלות איתן? היכן מוצאים נערות עובדות? בימינו בילוי עם מצפים לספק גם את האישה במיטת עיסוי. כל גבר אוהב לראות הנאה אמיתית של אישה. סטטיסטית, יותר מ-60% מהנשים יכולות להתפנק רק באמצעות דירה דיסקרטית במרכז
5 Killer Quora Answers On Uk Online Grocery Shopping Sites
uk Online Grocery shopping sites (https://team119.Co.kr)
Ten Ways To Build Your Mazda 3 Key Empire key Mazda
For most recent news you have to pay a quick visit the web and on web I found this site as
a best web page for newest updates.
The Comprehensive Guide To Examples Of Online Shopping Smart Security Camera
See What Replacement Window Seals Tricks The Celebs Are Making Use Of replacement window Seals
Hi, after reading this awesome paragraph i am too delighted to share my knowledge here
with friends.
This Story Behind Key Ignition Replacement Can Haunt You Forever!
car key Ignition replacement near me (library.kemu.ac.ke)
This is the right webpage for everyone who would like to understand
this topic. You understand a whole lot its almost hard to
argue with you (not that I personally will need to…HaHa).
You definitely put a brand new spin on a subject that’s been discussed for a long
time. Wonderful stuff, just great!
Gracias por compartir tus conocimientos de manera tan entretenida y educativa.
¡Estoy muy asombrado!
Have you ever thought about writing an e-book or guest authoring on other blogs?
I have a blog based upon on the same topics you
discuss and would really like to have you share some stories/information. I know my viewers would value your work.
If you are even remotely interested, feel free to shoot me an email.
Qué excelentemente organizado está tu texto; es un goce
repasar algo tan bien estructurado.
Your means of describing everything in this piece of writing
is really pleasant, every one can simply understand it, Thanks a lot https://missgriffintown.com/promotions-2/
Есть граница до которой допускаю слив, а дальше перерыв.
Thanks a lot, Loads of material!
WOW just what I was looking for. Came here by searching for
click through the up coming website page
The Reason Why Hyundai I20 Key Fob Replacement Is Everyone’s
Obsession In 2023 repairing
Thank you for any other magnificent article. The place else may just
anybody get that kind of information in such a perfect approach of writing?
I’ve a presentation next week, and I’m on the look for such information.
10 Tips For Interactive Slots That Are Unexpected Mobile Slots