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
10 Of The Top Mobile Apps To 1kg Coffee Beans 1Kg Of Coffee Beans
We stumbled over here from a different web address
and thought I should check things out. I like what I see so now i am following you.
Look forward to going over your web page again.
My webpage – where is provadent manufactured
Truly a good deal of fantastic info.
Also visit my site – https://mostbet-bk.cz
20 Tools That Will Make You More Efficient With Best
Folding Treadmill Small Space best folding treadmill for small
space (fakenews.win)
Hello, all is going sound here and ofcourse every one
is sharing information, that’s in fact excellent, keep
up writing.
Познакомьтесь вместе с нашим профессиональным коллективом, яже создаёт уникальную мебель, воплощая ваши грезы в течение реальность http://www.cehitae2kuhnishki.ru/.
12 Facts About Asbestos Attorney To Make You Seek Out Other People asbestos attorneys; Brockca.Com,
вывод из запоя цена вывод из запоя цена .
Профессиональный сервисный центр где ремонтируют телефоны сервис смартфонов
A Glimpse Inside Realistic Love Dolls’s Secrets Of Realistic Love Dolls real Dollsex
Thanks a lot, I like it.
Feel free to visit my site :: https://mostbet-bk.de
india online pharmacy reputable indian online pharmacy п»їlegitimate online pharmacies india
Looking Into The Future How Will The Used Mobility Scooters For Sale Near Me Industry Look Like In 10 Years?new mobility scooters for sale near Me
Why You Should Not Think About Making Improvements To Your Tilt And Turn Window Not Closing Tilt And Turn Window Not Closing Properly
watch stories watch stories .
10 Easy Steps To Start Your Own Pragmatic Business 프라그마틱 무료스핀
Профессиональный сервисный центр ремонт телефон ремонт телефона
10 Things We All Love About Sectional Couch With Recliner modern sectional couch (Sherrie)
What’s The Job Market For Best 2 In 1 Pushchair Professionals?best 2 In 1 pushchair
Hello there, You’ve done an incredible job.
I’ll definitely digg it and personally suggest to my friends.
I am sure they’ll be benefited from this web site.
Для убедительности в честности
казино, клиенты могут пройти простую
процедуру регистрации и стать полноправными
членами клуба.
Профессиональный сервисный центр ремонт телефон ремонт старых телефонов
10 Startups That Are Set To Revolutionize The Sports Toto 4d Latest Result Industry For The Better 토토사이트 모음; https://Sociallweb.Com/,
india online pharmacy mail order pharmacy india best online pharmacy india
купить диплом о высшем образовании в уфе купить диплом о высшем образовании в уфе .
best online pharmacies in mexico medicine in mexico pharmacies purple pharmacy mexico price list
This Is A Guide To Auto Locksmiths Near Me In 2023 auto key fob Locksmith near Me
See What Private Adult ADHD Assessment UK Tricks The Celebs Are Utilizing Private Adult Adhd Assessment Uk (Sociallytraffic.Com)
The Unknown Benefits Of Black Electric Wheelchair power chairs for Handicapped
Quality content is the important to interest the people to visit
the web site, that’s what this web page is providing.