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
15 Best Pinterest Boards Of All Time About Slot Animations top-rated Online Slots
купить аттестат за 11 класс http://www.ukr-diplom.ru .
купить диплом в екатеринбурге купить диплом в екатеринбурге .
I feel that is one of the such a lot significant info for me.
And i am glad studying your article. However wanna remark on some normal things,
The site style is great, the articles is truly great : D.
Just right job, cheers
купить диплом юриста купить диплом юриста .
There’s definately a lot to find out about this issue. I like all of the points you’ve made.
A Positive Rant Concerning Online Shopping Sites Clothes Cheap Shopping Online Uk Websites (Scfoodville.Com)
How To Explain Replacement Double Glazed Window To A Five-Year-Old replacement double glazed windows
I think the admin of this site is in fact working hard in favor of
his web site, since here every material is quality based information.
купить диплом парикмахера купить диплом парикмахера .
Can I simply just say what a comfort to find someone that really understands what they are talking about on the web.
You actually know how to bring a problem to light and make it important.
More and more people ought to read this and understand this side of your story.
I was surprised that you’re not more popular because you most
certainly have the gift.
14 Misconceptions Common To Jackpot Slots random jackpot slots
Pills information for patients. Brand names.
can you buy cheap singulair prices
Some information about drugs. Read here.
5 Arguments Online Shopping Uk Electronics Is Actually
A Positive Thing durable ironing board 124x45cm
The 10 Most Scariest Things About Classic Casino Slots classic casino slots
Oh my goodness! Awesome article dude! Thanks, However
I am going through issues with your RSS. I don’t understand
the reason why I can’t join it. Is there anybody getting identical RSS problems?
Anybody who knows the answer will you kindly respond?
Thanks!!
15 Best Accident Compensation Bloggers You Need To Follow Accident Lawyer (jejucordelia.com)
If you are young now then you must know about college girls, how hot college girls are, if you want to have fun with them then let me tell you that all the college girls come here to have fun, you can have fun here and fulfill all your desires which they are doing now in college life.
look here: Aerocity Escorts Service
You’ll Never Guess This Window Doctor Near Me’s Benefits
window doctor Near Me
11 Creative Ways To Write About Shopping Online Sites List Vimeo
I do believe all the ideas you have offered in your post.
They’re really convincing and will definitely work. Still, the posts
are very short for beginners. May you please lengthen them a bit from next time?
Thank you for the post.
купить диплом дизайнера купить диплом дизайнера .
10 Locations Where You Can Find Penny Slots top casino slots
10 Unexpected Birth Injury Claim Tips birth injury attorney
Ten Winning Slots That Will Actually Make Your Life Better Online Slot Machines, https://Www.Mazafakas.Com/User/Profile/4194892,
Drug information sheet. Effects of Drug Abuse.
buy cheap phenergan no prescription
Some news about pills. Read here.
남자친구 크기 아는 법
남자 친구 의 크기를 알아보는 최고의 가이드
There’s definately a great deal to learn about this issue.
I love all of the points you made.
Seo Optimization: A Simple Definition search Engine optimization website
5 Lessons You Can Learn From Federal Employers lawsuit