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
Five Essential Tools Everyone In The Best French Door Fridge Freezer Industry Should Be Utilizing french Door Fridge freezers uk
В магазине сейфов предлагают сейф купить москва сейф купить
The 10 Most Terrifying Things About Titration ADHD Meds titration adhd meds
нарколог на дом вывод из запоя на дому нарколог на дом вывод из запоя на дому .
Say “Yes” To These 5 Bandar Online Togel Tips Togel4D
Guide To Situs Toto Togel: The Intermediate Guide On Situs Toto Togel situs Toto
I visited several web sites except the audio quality for audio songs present at this site is really superb.
В магазине сейфов предлагают купить сейф сейф купить в москве
What Is The Reason? Audi A5 Replacement Key Is Fast Becoming The Hot Trend For 2023 Replacement Audi A3 Key
срочный вывод из запоя ростов срочный вывод из запоя ростов .
Eventually, this is your very own choice and it’s possibly a discussion you
require 401k rollover to ira (https://awaregift.com/classificadoseanuncios/index.php?page=user&action=pub_profile&id=12692) have with your financial expert.
A New Trend In Wood Burning Stoves Uk best wood burning backpacking stove (Lou)
this tronlinkpro
This Is The History Of Electric Trikes In 10 Milestones
Indoor Electric Mobility Scooter
I every time spent my half an hour to read this blog’s articles
everyday along with a cup of coffee.
Magnificent beat ! I wish to apprentice while you amend your web site, how
could i subscribe for a blog website? The account helped me a acceptable
deal. I had been tiny bit acquainted of this your broadcast offered bright clear concept
11 Methods To Totally Defeat Your Car Lock Smith Car Key Mobile Locksmith
20 Inspiring Quotes About Affordable Bunk Beds For Kids bunk beds for kids with stairs
10 Beautiful Graphics About Coffee Pods Machine coffee pods machines
The Main Issue With Diagnosis For ADHD And How You Can Solve It who can diagnose Adhd In adults
15 Facts Your Boss Wished You’d Known About Biofuel Fire bio ethanol fire place – Alanna,
Attractive element of content. I simply stumbled upon your weblog and in accession capital to claim that I
acquire in fact loved account your weblog posts. Any way I will be subscribing to
your feeds and even I success you access consistently fast.
5 Killer Quora Answers On Double Glazed Near Me Double Glazed Near Me
The Top Companies Not To Be Watch In The Adhd Assessment In Adults Industry adhd assessments for Adults
See What Doctor Windows Tricks The Celebs Are Utilizing doctor window (diggerslist.com)
Good day! This post could not be written any better! Reading through this post reminds me
of my good old room mate! He always kept talking about this.
I will forward this write-up to him. Pretty sure he will have a good read.
Many thanks for sharing!
buy cheap ozempic ozempic online buy ozempic
https://creative-seal-dc4vll.mystrikingly.com/blog/14454604242
https://ozempic.art/# buy cheap ozempic
Fastidious answer back in return of this issue with solid arguments and explaining the whole thing regarding that.