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
Купить диплом магистра оказалось возможно, быстрое обучение и диплом на руки
damansara heights house for sale
ivermectin human: IverFast – buy ivermectin uk
Hi, i believe that i noticed you visited my blog so i got here to go
back the prefer?.I’m trying to in finding issues to improve my site!I
suppose its adequate to use some of your ideas!!
Howdy! I know this is kinda off topic nevertheless I’d figured
I’d ask. Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa?
My site goes over a lot of the same subjects as yours and I
believe we could greatly benefit from each other. If you
are interested feel free to send me an e-mail. I look
forward to hearing from you! Terrific blog
by the way!
Пошаговая инструкция по официальной покупке диплома о высшем образовании
forexsnews.ru/sdelayte-shag-vpered-poluchite-diplom-byistro
I have read so many content regarding the blogger lovers however this article is actually a pleasant piece of
writing, keep it up.
https://klccestate.com/property/bangsar-bungalow/
Как оказалось, купить диплом кандидата наук не так уж и сложно
Как официально купить аттестат 11 класса с упрощенным обучением в Москве
I’d like to find out more? I’d like to find out more details.
Abortion pills online buy misoprostol over the counter order cytotec online
новые онлайн займы
generic plavix PlavixClo Cost of Plavix on Medicare
http://www.taxipuma.ru — качество и стиль от производителя
Appreciating all these referrals customized in the direction of particular areas– really thoughtful technique!” ### anyKeyWord Hill Country Flooring & Construction
Как купить аттестат 11 класса с официальным упрощенным обучением в Москве
https://stanproperty.com/property/Bangsar-Bungalow-For-Sale2/
Как получить диплом о среднем образовании в Москве и других городах
Great article! I’ve been looking for a reliable roofing company and your tips are really helpful licensed roofing contractors near me
buy Lisinopril online: lisinopril1st – Lisinopril 1st
Drug information for patients. Short-Term Effects.
cost of zithromax without dr prescription
Actual news about meds. Read information here.
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
https://lisinopril1st.com/# lisinopril 4 mg
dapoxetine price: cheap priligy – buy priligy
This is very attention-grabbing, You are a very professional blogger.
I have joined your feed and stay up for in quest of extra of your wonderful post.
Also, I have shared your web site in my social networks
https://cytpremium.com/# cytotec pills buy online
Официальное получение диплома техникума с упрощенным обучением в Москве
https://klccestate.com/bungalow/
Официальная покупка диплома вуза с сокращенной программой обучения в Москве