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
Paragraph writing is also a excitement, if you be acquainted with then you can write otherwise
it is complicated to write.
10 Graphics Inspirational About Kia Sportage Key Fob
Kia Smart Key Programming
The 10 Scariest Things About Rush Sugar Demo rush Sugar Demo
How Double Glazed Replacement Glass Near Me Was The Most Talked About Trend Of 2023 Double glazing near me
Nice post. I was checking continuously this blog and I am impressed!
Very useful info specifically the last part 🙂 I care for such information much.
I was seeking this certain information for a very long time.
Thank you and best of luck.
10 Things We Are Hating About Double Glazing Near Me double glazing Company near me
Medicament information sheet. Long-Term Effects.
where buy generic azathioprine
Some what you want to know about medicine. Read information now.
Nine Things That Your Parent Teach You About Double Glazed Window Suppliers Near Me double
glazed window suppliers near me (Marcel)
What’s The Job Market For Fela Railroad Accident Lawyer Professionals Like?
Fela railroad accident lawyer
Meds information sheet. Long-Term Effects.
buying cheap nortriptyline tablets
All what you want to know about medicines. Read now.
Ways Of Deciding On Your Seo Firm 검색엔진최적화 seo
Helpful info. Fortunate me I discovered your site by chance,
and I am shocked why this coincidence did not took place in advance!
I bookmarked it.
How To Save Money On Psychiatry Assessment Uk Assessment In psychiatry
Drugs information sheet. What side effects can this medication cause?
buying generic sumatriptan for sale
Everything what you want to know about drugs. Get information here.
Приветствую. Подскажите, где найти полезные блоги о недвижимости? Пока нашел https://vortex-los.ru
I was recommended this website through my cousin. I am now not sure
whether or not this post is written by means of him as nobody else know such
distinct about my difficulty. You’re amazing! Thanks!
Medicine information for patients. Long-Term Effects.
cheap xenical without insurance
Some about drug. Read information here.
4 Dirty Little Secrets About The Veterans Disability Litigation Industry Attorney
You’ll Never Guess This Double Glaze Repair Near Me’s Secrets double glaze repair near me (http://hotelrocio.kr/bbs/board.php?bo_table=free&wr_id=69493)
Guide To Double Glazed Window Near Me: The Intermediate Guide The
Steps To Double Glazed Window Near Me double glazed window near me
What Experts From The Field Want You To Know? 023456789
Hi there, I desire to subscribe for this webpage to obtain latest updates, therefore where can i do it please help out.
Приветствую. Может кто знает, где почитать разные блоги о недвижимости? Пока нашел https://vortex-los.ru
Seven Explanations On Why Veterans Disability Case Is
Important veterans disability lawyer
You Can Explain Collapsible Scooter To Your Mom arlennizo
Medicine information for patients. Generic Name.
can i purchase atarax for sale
All trends of medication. Read here.
How To Research Medical Malpractice Lawsuit Online princeton Medical malpractice lawyer
Medicine information for patients. Long-Term Effects.
how can i get cheap dilantin without rx
Best information about pills. Get information here.
Medication information leaflet. What side effects can this medication cause?
how to get cheap lisinopril pills
Some trends of drugs. Read information now.
Hey There. I found your blog using msn. This is an extremely well
written article. I’ll be sure to bookmark it and come back to read more of
your useful info. Thanks for the post. I will certainly return.