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
Послушайте.
due to the effect of lower prices it is important that https://bitcoin-recovery.io/ always very expensive for beginners.
I am really impressed together with your writing abilities
and also with the structure on your blog. Is that this a paid topic or did you customize it yourself?
Anyway stay up the excellent high quality writing, it’s uncommon to look a nice
weblog like this one today..
Great job! Find more at real estate nearby
сколько стоит сделать загранпаспорт сколько стоит сделать загранпаспорт .
загранпаспорт срочно загранпаспорт срочно .
срочное оформление загранпаспорта срочное оформление загранпаспорта .
It’s remarkable designed for me to have a web page, which is useful in support of my
experience. thanks admin
срочное оформление загранпаспорта срочное оформление загранпаспорта .
Appreciate it. Plenty of knowledge.
Mighty Dog Roofing
Reimer Driive North 13768
Maplle Grove, MN 55311 United Տtates
(763) 280-5115
affordable storm restorations
https://maxpillsformen.com/# Cialis without a doctor prescription
Viagra generic over the counter viagra canada Cheap Viagra 100mg
Thanks for the comprehensive read. Find more at outdoor lighting near me
It’s going tߋ be finish of mine day, however before end
I am reading this great pіece of writing to improve my knowledge.
Feel free to visit my page – matic car rental bandung
How To Live The Annual Office Party – 10 Suggestions 출장안마 (j-chin.music.coocan.jp)
срочное оформление загранпаспорта срочное оформление загранпаспорта .
срочно сделать загранпаспорт https://guryevsk.forum24.ru/?1-4-0-00000570-000-0-0-1735286442/ .
Buy generic 100mg Viagra online: FastPillsForMen – sildenafil online
Hey There. I found your blog using msn. This is a really well written article.
I will be sure to bookmark it and return to
read more of your useful info. Thanks for the post. I will certainly
comeback.
The Ultimate Guide For The Ultimate Nightlife Destinations 하이오피
10 Misconceptions Your Boss Has About Search Engine Optimization Agency Search engine optimisation manchester
Generic Cialis without a doctor prescription Cialis over the counter п»їcialis generic
купить диплом в иркутске
cheapest online ed treatment cheap cialis cheapest ed medication
https://fastpillseasy.com/# online ed pharmacy
5 1 Tip For Marketing Your Online Business 유흥
Hello mates, how is all, and what you wish for to say on the topic of this
paragraph, in my view its genuinely amazing in support
of me.
The Companies That Are The Least Well-Known To In The Car Accident Attorneys Near Me Industry
lawyer for car accidents near me (Blanca)
Thanks for the thorough article. Find more at access platforms
http://fastpillsformen.com/# Sildenafil Citrate Tablets 100mg