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
Thanks for the clear advice. More at beauty parlour near me
Went into ### anykeyword ### today they usually were wonderful friendly!! Thank you all such a Best phone repair shop
Drug information sheet. What side effects can this medication cause?
get cheap zithromax no prescription
Some news about pills. Read information here.
The art of floral setup is absolutely an expression of imagination! I ‘d love to find out more concerning it– where can I locate ideas? See me at local florist delivery for
What’s up it’s me, I am also visiting this site regularly, this website is
genuinely nice and the users are actually sharing good thoughts. http://secretour.xyz/bbs/board.php?bo_table=free&wr_id=179348
카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트
카지노사이트 카지노사이트
where to buy erectile dysfunction pills: cheap cialis – buying ed pills online
penis enlargement
Where Will Remote Key Fob Repair One Year From Today?
Remote Key Fob Repairs
Всё о покупке аттестата о среднем образовании: полезные советы
where to get ed pills: fast pills easy – cheap ed
essentially just a archive of transaction data, often referred to as a distributed accounting, which is duplicated and broadcast to a network of https://https://gamestop-wallet.io// users who are ready to control and coordinate data databases.
Drugs information. Drug Class.
where can i buy cheap phenergan prices
Some what you want to know about medication. Get information here.
Cocktail Lounge 역삼오피
For latest news you have to visit web and on the web I found this web page as a best website for
most up-to-date updates.
Generic Tadalafil 20mg price MaxPillsForMen buy cialis pill
Mighty Dog Roofing
Reimer Druve North 13768
Maple Grove, MN 55311 Unitesd Ꮪtates
(763) 280-5115
seamless siding replacements
I found this very interesting. For more, visit Cypress Pro Wash – pressure washing company
5 Laws Anybody Working In Peritoneal Mesothelioma
Asbestos Should Be Aware Of Mesothelioma Attorneys
viagra without prescription: FastPillsForMen.com – over the counter sildenafil
15 Gifts For The Shipping Containers Lover In Your Life 20ft shipping Containers
https://maxpillsformen.com/# Buy Cialis online
This was a great help. Check out access platforms for more
This was highly helpful. For more, visit scaffolding
вывод из запоя цены ростов-на-дону вывод из запоя цены ростов-на-дону .
выезд на дом капельница от запоя выезд на дом капельница от запоя .
Hello, i read your blog from time to time and i own a similar one and
i was just curious if you get a lot of spam comments?
If so how do you protect against it, any plugin or anything
you can advise? I get so much lately it’s driving me
crazy so any support is very much appreciated.
ambulation,ラブドール オナホfeeding,
Купить диплом магистра в Кирове
https://fastpillsformen.com/# best price for viagra 100mg