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
See What Cheapest Automatic Folding Mobility Scooter Tricks The
Celebs Are Using cheapest automatic folding Mobility Scooter
indian pharmacies safe India pharmacy international mail order pharmacy india
Medicines information sheet. Cautions.
famotidine use and thrombocytopenia in the trauma patient
Some about pills. Get information here.
The 10 Most Scariest Things About Best Infant Cribs best
infant cribs (https://Www.google.com.Pe/)
Быстрое обучение и получение диплома магистра – возможно ли это?
Replacement Audi Key 10 Things I Wish I’d Known In The Past replacement audi keys
How Birmingham Window Repair Became The Hottest Trend In 2023 Door Repair Birmingham
I was suggested this website by my cousin. I am no longer positive whether
or not this put up is written by way of him as no one else know such
precise about my difficulty. You are amazing! Thank you!
People realized that by searching for pharmacy warfarin clinic today!
Think You’re Cut Out For Doing Fireplace Suite? Take This Quiz electric fireplace with wood mantel (Noreen)
9 . What Your Parents Taught You About Audi Tt Key audi tt Key
Guide To Skoda Key Fob Replacement: The Intermediate Guide Towards Skoda Key Fob Replacement
skoda key fob replacement (Mack)
20 Things That Only The Most Devoted Frost Free Fridge Freezer 50/50 Fans Should Know Extra tall fridge freezer 50/50
купить диплом итмо [url=https://diplomdarom.ru/]diplomdarom.ru[/url] .
Покупка диплома о среднем полном образовании: как избежать мошенничества?
plastdet.ru/index.php?topic=377.new#new
All The Details Of Audi Car Key Dos And Don’ts audi key replacement cost, http://79bo.com/space-uid-6820349.html,
5 Killer Quora Answers To Cost Of Tilt And Turn Upvc Windows cost of tilt and turn upvc windows
What Mesothelioma Law Experts Want You To Learn Mesothelioma law expert
The One Pragmatic Trick Every Person Should Be Aware Of 라이브 카지노
(Julianne)
I go to see each day a few web pages and blogs to read articles, except this webpage gives quality based articles.
Amazing! This blog looks just like my old one! It’s on a entirely different subject but it has pretty much the same layout and design.
Excellent choice of colors!
cheap drugs: low cost prescription – drug prices
Why Nobody Cares About Porsche Macan Key Replacement Cost New Porsche Key Cost
best natural ed treatment: cheaper medications – prescription drugs online
Freestanding Electric Fireplace Techniques To Simplify Your Daily Life
Freestanding Electric Fireplace Trick Every Person Should Learn freestanding electric fireplace
Приобретение школьного аттестата с официальным упрощенным обучением в Москве
Very energetic article, I enjoyed that bit.
Will there be a part 2?
Will Virtual Mystery Boxes One Day Rule The World?
mystery box Website
15 Things You’re Not Sure Of About Online Mystery Boxes mystery Box website
купить диплом производство man-diploms.ru .