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
Its like you read my mind! You appear to know so much
about this, like you wrote the book in it or something.
I think that you can do with a few pics to drive the message
home a little bit, but other than that, this is great blog.
An excellent read. I’ll certainly be back.
http://sildenafiliq.com/# Viagra online price
http://tadalafiliq.shop/# Generic Tadalafil 20mg price
https://tadalafiliq.shop/# Buy Cialis online
https://tadalafiliq.shop/# Generic Cialis without a doctor prescription
https://kamagraiq.shop/# buy Kamagra
http://sildenafiliq.com/# Cheap generic Viagra online
Kamagra 100mg: Sildenafil Oral Jelly – п»їkamagra
очень важно. От ей-богу.
Also visit my web blog … ノンケ 手コキ
This is a very good tip especially to those fresh
to the blogosphere. Simple but very accurate info… Many thanks for sharing this one.
A must read article!
Cheap Viagra 100mg: sildenafil iq – Generic Viagra online
alternativa159.ru
best price for viagra 100mg: cheapest viagra – Viagra without a doctor prescription Canada
There are a variety of butt plugs on the market.
It can be difficult to select the one that is right for
you. Butt
plugs should last and safe to use for a broad range of.
There are plenty of great alternatives.
Spot on with this write-up, I absolutely feel this website needs far
more attention. I’ll probably be returning to read through more, thanks for the advice!
buy cialis pill: Generic Tadalafil 20mg price – cheapest cialis
Thank you a bunch for sharing this with all of us you actually know what you are speaking approximately!
Bookmarked. Kindly additionally visit my website =).
We can have a hyperlink alternate agreement among us
The name of the author is Roseline Odum can make is not
her birth name. The job he’s been occupying for years is a production and
planning police officer. I am really fond of to watch out movies and I’ve
been doing it for some time. New Jersey is the place
he loves most and his family loves it.
Piece of writing writing is also a fun, if you be acquainted with afterward you can write
if not it is complicated to write.
How To Explain Dripper Coffee Maker To Your Grandparents
coffee drip Machine
https://tadalafiliq.shop/# buy cialis pill
generic sildenafil: best price on viagra – viagra canada
It’s The Complete List Of Erb’s Palsy Lawyers Dos And Don’ts Erb’s Palsy
Lawyer (http://Kbphone.Co.Kr/Bbs/Board.Php?Bo_Table=Free&Wr_Id=656815)
http://kamagraiq.shop/# buy kamagra online usa
https://sildenafiliq.xyz/# Sildenafil Citrate Tablets 100mg
http://sildenafiliq.com/# Sildenafil Citrate Tablets 100mg
We’re a group of volunteers and opening a new scheme in our community.
Your web site provided us with valuable information to work on. You’ve done an impressive job and
our entire community will be grateful to you.
14 Smart Ways To Spend Leftover Accident Compensation Budget accident lawsuit
Why Truck Accidents Lawyers Is The Next Big Obsession truck Accident Law firms
Medication information sheet. Long-Term Effects.
zoloft buy
Everything about meds. Read information now.