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
Официальная покупка аттестата о среднем образовании в Москве и других городах
Как не стать жертвой мошенников при покупке диплома о среднем полном образовании
Cảm giác như mình trở thành một phần của cộng đồng khi chơi ở Hitclub! HitClub
I’ve been exploring for a little bit for any high
quality articles or weblog posts on this sort of house .
Exploring in Yahoo I eventually stumbled upon this site.
Reading this information So i’m happy to show that I’ve a very just
right uncanny feeling I found out just what I
needed. I most no doubt will make sure to do not fail to remember this site and give it a look regularly.
It’s enormous that you are getting thoughts from
this article as well as from our dialogue
made here.
my page :: realtor in tucson arizona
https://pinup-kazi.ru/# pinup kazi
вавада: vavada – вавада казино
darknet drug links deep dark web dark web sites links
Medicament information sheet. Short-Term Effects.
cost of generic irbesartan without insurance
Actual about pills. Read here.
Реально ли приобрести диплом стоматолога? Основные шаги
актуальные фрироллы jack poker
https://t.me/s/jack_poker_frirollyi
пин ап казино официальный сайт: pinup-kazi.ru – пин ап казино
Приобретение школьного аттестата с официальным упрощенным обучением в Москве
Your style is very unique best realtor in saint augustine florida comparison to other folks I have read stuff from.
I appreciate you for posting when you have the opportunity,
Guess I’ll just book mark this site.
I loved as much as you’ll receive carried out right here.
The sketch is attractive, your authored
subject matter stylish. nonetheless, you command get got an impatience over that you wish
be delivering the following. unwell unquestionably
come more formerly again as exactly the same nearly a lot often inside case
you shield this hike.
Here is my blog – warm beverage hoodies
https://www.larson-holz-spb.ru – ваш надежный помощник в выборе и заказе кухонной мебели.
pinup пинап казино pinup-kazi.ru
Night Out 유흥 (bridgejelly71>Fusi.serena@www.woostersource.Co.uk)
Everything is very open with a clear clarification of the challenges.
It was really informative. Your website is extremely helpful.
Thanks for sharing!
Покупка диплома о среднем полном образовании: как избежать мошенничества?
Как приобрести диплом о среднем образовании в Москве и других городах
Greetings from Ohio! I’m bored to tears at work so I decided to check out your site on my
iphone during lunch break. I really like the knowledge you present here and can’t wait to take a look when I get home.
I’m surprised at how quick your blog loaded on my cell phone ..
I’m not even using WIFI, just 3G .. Anyhow, wonderful site!
Hmm is anyone else having problems with the pictures on this blog loading?
I’m trying to determine if its a problem on my end or
if it’s the blog. Any feed-back would be greatly appreciated.
Feel free to visit my website :: realtor in volusia county fl
darknet drug links darknet market links blackweb official website
пин ап кз: пинап казино – pinup-kazi.kz
get generic lisinopril online
пин ап кз: pinup kazi – pinup
Locating the appropriate SR22 insurance coverage is actually important
to avoid high costs. Try to find discounts and contrast quotes to discover budget friendly
SR22 insurance options.
Полезные советы по безопасной покупке диплома о высшем образовании
Howdy! This article could not be written much better!
Looking through this post reminds me of my previous
roommate! He constantly kept talking about this. I am going to forward this article
to him. Fairly certain he’ll have a great read. Thank you for sharing!