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
What’s Taking place i’m new to this, I stumbled upon this
I’ve found It absolutely useful and it has helped me
out loads. I’m hoping to give a contribution & aid other customers
like its helped me. Great job.
Mesothelioma lawyers aid the families of victims throughout the legal process.
They prepare cases, file lawsuits, and asbestos settlement trust claims they
negotiate settlements and fight for fair verdicts in trial.
Macrobid: giant eagle pharmacy hours – boots pharmacy antibiotic eye drops chloramphenicol
Since the admin of this site is working, no question very shortly it will be renowned, due to its feature contents.
Great beat ! I wish to apprentice while you
amend your site, how could i subscribe for a blog web site?
The account helped me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear
idea
Highly descriptive blog, I liked that a lot. Will there be a part 2?
Как официально приобрести аттестат 11 класса с минимальными затратами времени
trazodone price pharmacy: pharmacy online mexico – compare prescription prices
24 hour pharmacy: antibacterial – tretinoin cream pharmacy
The 10 Most Scariest Things About Situs Toto Login situs toto
login, https://www.98e.fun/,
20 Tips To Help You Be More Effective At Lexus Key
Replacement Uk cost to replace lexus key fob
– willysforsale.com –
buy priligy online pharmacy: AebgEmoli – accredo pharmacy
Five Killer Quora Answers On Sofas Sale sofas Sale
Opened up interesting material – I recommend sharing this discovery http://samara.listbb.ru/viewtopic.php?f=3&t=512
ad rx pharmacy: neurontin pharmacy assistance – Motilium
Titration ADHD Meds Tools To Improve Your Daily Lifethe One Titration ADHD Meds Trick Every Person Should
Know Titration adhd Meds
Ten Adhd Adult Assessment That Will Actually Change Your Life adhd Assessment Uk adults
See What Best Meds For Anxiety Disorders Tricks The Celebs Are Using best meds for anxiety disorders
good neighbor pharmacy loratadine: viagra american pharmacy – pompharmacy viagra
Nice post. I learn something totally new and challenging
on sites I stumbleupon every day. It will always be useful to
read content from other authors and use a little something from their sites.
An intriguing discussion is worth comment. I think that you need
to write more on this topic, it may not be a taboo
subject but usually folks don’t discuss such subjects. To the next!
Many thanks!!
online slot
11 Creative Ways To Write About Private Psychiatrist Colchester can a private psychiatrist prescribe medication
мужской ретрит https://ретриты.рф
ретриты 2024 https://ретриты.рф
психологический ретрит https://ретриты.рф
It’s perfect time to make some plans for the future and it is time to be
happy. I’ve read this post and if I could I desire to suggest you few interesting things or
advice. Maybe you can write next articles referring to this article.
I want to read even more things about it!
Right now it seems like Movable Type is the preferred blogging platform available right
now. (from what I’ve read) Is that what you are using on your blog?
https://thoughtful-sparrow-dc4vlm.mystrikingly.com/blog/ee0123e3c71
How Much Do Remote Control Anal Plug Experts Make? Anal Jewelry