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
Если вы искали где отремонтировать сломаную технику, обратите внимание – профи услуги
A baby twitching while falling asleep can have as numerous as 100
convulsions a day.
Five Killer Quora Answers To Situs Terpercaya situs Terpercaya
부산 유흥메카 연산동 룸알바에서 큰 돈 한번 만져 보실래요?
10 Times You’ll Have To Know About Daftar Situs Togel togel sydney
I do not even know how I ended up here, but I thought this post was great.
I do not know who you are but definitely you’re
going to a famous blogger if you aren’t already 😉 Cheers!
vet pharmacy online: propranolol online pharmacy – medicine online shopping
Why Car Key Programmer Near Me Will Be Your Next Big Obsession? Auto key programmer
Если вы искали где отремонтировать сломаную технику, обратите внимание – техпрофи
I like it whenever people get together and share ideas. Great website, keep it
up!
ретрит для женщин https://ретриты.рф
h-e-b pharmacy: letrozole online pharmacy – omeprazole online pharmacy
Paragraph writing is also a fun, if you be acquainted
with after that you can write or else it is complex to write.
Foldaway Electric Wheelchair Tools To Make Your Daily Life Foldaway Electric Wheelchair Trick Every Individual Should Know foldaway electric wheelchair
ретрит для женщин https://ретриты.рф
When some one searches for his essential thing, therefore he/she desires to
be available that in detail, so that thing is maintained
over here.
Could Programming Keys Be The Key For 2023’s
Challenges? programming car keys
pharmacy in artane castle: online pharmacy certification – Nortriptyline
plavix pharmacy assistance: cytotec online pharmacy – viagra kuwait pharmacy
See What Erbs Palsy Attorney Tricks The Celebs Are Using erbs Palsy attorney
Excellent way of describing, and good paragraph to obtain facts on the topic of my
presentation topic, which i am going to present in institution of higher education.
психологический ретрит https://ретриты.рф
Сколько стоит получить диплом высшего и среднего образования легально?
ретрит туры https://ретриты.рф
Wow, this article is pleasant, my younger sister is analyzing these kinds of things,
therefore I am going to tell her.
Thanks in support of sharing such a good idea,
post is pleasant, thats why i have read it fully
Have a look at my page my link
viagra pharmacy online: propecia at boots pharmacy – tricare pharmacy crestor
Fucked out of my knickers.
One day I was travelling with my brother Artyom in the car.
He’s a year older than me. We were following his friends to celebrate his driving licence.
The party was going to be at the dacha. Artem told me to sit in the back seat when his three friends got in. I was sandwiched between two of them.
Stop by my website :: Fuck
Feel free to visit my webpage; Find top-rated certified Daycares in your area
With havin so much written content do you ever run into any issues of plagorism or
copyright infringement? My site has a lot of completely unique content I’ve either written myself or outsourced but it seems a
lot of it is popping it up all over the internet without my
authorization. Do you know any solutions to help reduce
content from being stolen? I’d truly appreciate it.