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
buy generic ciprofloxacin buy cipro ciprofloxacin over the counter
Upvc Repairs Near Me Tips To Relax Your Daily Lifethe One Upvc Repairs Near
Me Trick That Everybody Should Be Able To upvc repairs near Me
Hi there, just became alert to your blog through Google, and found that it’s
truly informative. I am gonna watch out for brussels.
I’ll be grateful if you continue this in future.
Numerous people will be benefited from your writing. Cheers!
If some one needs expert view regarding blogging afterward i suggest him/her to go to see this blog, Keep up the good job.
What To Look For In The Right Designer Handbags Sale For
You Designer Handbags Popular
https://lisinopril.network/# cost for 2 mg lisinopril
lisinopril 104 lisinopril 20 mg buy prices for lisinopril
10 Myths Your Boss Is Spreading Concerning New Avon Book avon book December 2023; http://daywell.kr/bbs/board.php?bo_table=free&wr_id=419125,
15 Secretly Funny People Work In Wood Burning Stoves Uk
Heating Solutions
batmanapollo.ru
cost propecia without rx: cost of propecia prices – cost of generic propecia without rx
What Is Titration ADHD? History Of Titration ADHD
what Is titration adhd
The Most Popular Semi Truck Claim The Gurus Are Using 3 Things accident
9 . What Your Parents Taught You About Medical Malpractice Lawyer lawyer
11 Ways To Completely Revamp Your Folding Treadmills
treadmills fold up (Jacqueline)
https://finasteride.store/# cost of cheap propecia prices
http://ciprofloxacin.tech/# buy cipro online canada
http://lisinopril.network/# lisinopril 5 mg price in india
https://finasteride.store/# propecia tablets
azithromycin online sale
cost of cheap propecia tablets order propecia tablets order generic propecia without dr prescription
Link exchange is nothing else but it is only placing the other person’s website link on your page at appropriate
place and other person will also do same in support of you.
Here is my blog post; PuffWow
generic propecia: order cheap propecia price – get cheap propecia tablets
What’s The Reason Everyone Is Talking About Double Glazed Window Repair This Moment window repairs near Me
femara vs tamoxifen where can i buy nolvadex does tamoxifen cause weight loss
I always used to study piece of writing in news papers but now as I am a user of internet
thus from now I am using net for articles or reviews, thanks to web.
http://ciprofloxacin.tech/# ciprofloxacin over the counter
https://ciprofloxacin.tech/# ciprofloxacin mail online
rx lisinopril 10mg: lisinopril 10 mg tablet price – zestril pill
5 Killer Quora Answers To Online Shopping Uk Women’s Clothing Online Shopping Uk Women’s Clothing (Spacebohemian.Com)