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
Mesothelioma Talc Lawsuit
%%
My page – electrical certificate dunstable [Vernon]
Excellent post. I was checking continuously this blog
and I’m impressed! Extremely useful information specially the last part 🙂 I care for such information much.
I was seeking this particular information for
a long time. Thank you and best of luck.
mesothelioma attorneys Construction worker
https://bogin10.z28.web.core.windows.net/research/bogin10-(4).html
This should provide you with plenty of time to analysis, attempt on, order, and alter any dress you discover yourself purchasing.
https://storage.googleapis.com/bogin13/research/bogin13-(6).html
This consists of most variations of white, corresponding to ivory and champagne.
asbestos cancer lawsuit lawyer mesothelioma (asbestoslawyers34319.blogspothub.com)
https://storage.googleapis.com/chinavisa11/research/chinavisa11-(115).html
We even have tea-length dresses and lengthy attire to swimsuit any season, venue or preference.
%%
My web page 1500mg full spectrum cbd oil uk (find-Open.co.Uk)
https://bogin3.s3.us-west-004.backblazeb2.com/research/bogin3-(24).html
Jovani Plus size mother of the bride attire suits any body type.
claim Asbestos
Asbestos Claims
%%
Stop by my page – Coffee machine with Pods
https://bogin66.z32.web.core.windows.net/research/bogin66-(36).html
Some ladies favor to wear a costume, whereas others prefer separates…and each are great options!
https://bogin50.z1.web.core.windows.net/research/bogin50-(30).html
“I stated right then I would wear it in the future,” she recalled.
https://storage.googleapis.com/bogin25/research/bogin25-(33).html
If you haven’t heard from her by about five months before the wedding, don’t be afraid to achieve out and ask for an update on the gown code.
Very good blog! Do you have any suggestions for aspiring writers?
I’m planning to start my own blog soon but I’m a little lost on everything.
Would you suggest starting with a free platform like WordPress or go for a
paid option? There are so many choices out there that I’m totally overwhelmed ..
Any tips? Appreciate it!
Greetings, I believe your blog might be having
web browser compatibility problems. Whenever I take a look
at your web site in Safari, it looks fine but when opening in Internet Explorer, it’s got some overlapping issues.
I merely wanted to give you a quick heads up!
Other than that, wonderful website!
attorneys
asbestos mesothelioma lawyer – Luisa –
Your blog post was a rather delightful read, I must admit!
It’s not often you stumble upon something so captivating and insightful.
Your writing style has a certain charming simplicity that makes it a delight to
get lost in, and I truly appreciate that. The insights you’ve shared on this topic
are thought-provoking. It’s evident that you
have a comprehensive grasp of the subject matter, and I found your perspective to be quite refreshing.
Thank you for taking the time to share your wisdom
with your readers. I’m eagerly looking forward to your next post.
Keep up the excellent work, it’s truly appreciated!
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/bogin11/research/bogin11-(169).html
Cream is another choice or skirt and prime in a black and white mixture.
%%
My blog – keys for mercedes (Desmond)
Asbestos Claims Lawyers
chandal real madrid
Asbestos Lawsuit Payouts (Lawyers-Asbestos49991.Blogrelation.Com)
asbestos class action lawsuit
Ꭲhis is my first time pay а quick visit ɑt heree and
i amm trᥙly hsppy tto rеad all аt οne placе.
Here іs my webpage; Những mẫu đồng hồ nữ đẹp giá rẻ
%%
Here is my blog :: price
https://seo38.z10.web.core.windows.net/research/seo38-(51).html
With over five years of expertise in bridal, she is an professional on all issues fashion.