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
ozempic: ozempic – ozempic
http://ozempic.art/# buy cheap ozempic
continuously i used to read smaller articles which also clear their
motive, and that is also happening with this article
which I am reading now.
нарколог на дом вывод из запоя ростов нарколог на дом вывод из запоя ростов .
https://rybelsus.shop/# rybelsus pill
The Top Reasons Why People Succeed On The Coffee Pods Machine Industry Pod Coffee Machine
Five Killer Quora Answers On Toto4d toto4d
The Three Greatest Moments In Buy Tilt And Turn Windows History Buy Tilt And Turn Windows Online – Securityholes.Science –
https://rybelsus.shop/# rybelsus cost
вывод из запоя в ростове на дону вывод из запоя в ростове на дону .
9 Things Your Parents Taught You About Titration ADHD Medications titration Adhd Medications
What Is Foldable Wheelchairs For Sale And Why Is Everyone Speakin’ About
It? Lightweight Wheelchair Foldable
15 Inspiring Facts About Keys Programmed That You Never Known Car Key Programming And Cutting
What Is The Future Of Buy Realistic Sex Doll Be Like In 100 Years?
buy realistic sex dolls
Peculiar article, totally what I needed.
В магазине сейфов предлагают купить сейф в москве цена где купить сейф
Why You Should Concentrate On Improving Electric Fireplace Heater Freestanding retro electric fireplaces – Jay,
Свой Психолог сейчас
В магазине сейфов предлагают модели сейфов сейфы купить
buy rybelsus online cheapest rybelsus pills buy semaglutide pills
Thanks very interesting blog!
10 Meetups About Freestanding Electric Range You Should Attend best freestanding electric fireplaces (Madeleine)
Childrens Bunk Beds Tips To Relax Your Everyday Lifethe Only Childrens Bunk Beds Trick That Every Person Should Learn childrens bunk bed
5 Laws That Anyone Working In Audi Replacement Car Keys Should Know audi Replacement key cost
Ozempic without insurance ozempic cost ozempic coupon
The 9 Things Your Parents Taught You About Key Audi key Audi
http://rybelsus.shop/# rybelsus pill
24-Hours To Improve Sectional Sofa L Shaped L shaped sofa grey
Link Building Insider Tips – A Six-Step Generating 1 Way Links Strategic Plan 토렌트모음 (https://heavenarticle.com/author/waveitaly4-943039/)
Top Seo Myths – Part 1 백링크