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
insta viewer insta viewer .
Реально ли приобрести диплом стоматолога? Основные этапы
Why The Biggest “Myths” About Samsung Freezers Could Actually Be Accurate samsung Refrigerator models
ig viewer ig viewer .
5 Killer Quora Answers To ADHD Private Assessment UK Adhd private Assessment Uk
guvenilir casino siteleri: Casino Siteleri – casino siteleri win
Стоимость дипломов высшего и среднего образования и процесс их получения
I?m impressed, I must say. Seldom do I encounter a blog that?s both equally educative and interesting, and without a doubt, you have hit
the nail on the head. The problem is something which
not enough folks are speaking intelligently about.
Now i’m very happy that I stumbled across this during my search
for something regarding this.
Casino Siteleri: Casino Siteleri – Canl? Casino Siteleri
The 9 Things Your Parents Taught You About Single
Stroller With Bench Seat Single Stroller With Bench Seat
Покупка диплома о среднем полном образовании: как избежать мошенничества?
mir-kolyasok.ru/kupit-attestat-v-melitopole.html
deneme bonusu veren siteler mycbet.com: deneme bonusu veren siteler denemebonusu2026.com – denemebonusuverensiteler.top
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
Узнайте, как безопасно купить диплом о высшем образовании
10 Top Books On Case Battles case battle cs go (Gus)
Greetings! Very useful advice in this particular post!
It is the little changes which will make the largest changes.
Thanks for sharing!
Как купить аттестат 11 класса с официальным упрощенным обучением в Москве
go4pro.ru/kupit-attestat-za-9-klass-2023.html
Всё, что нужно знать о покупке аттестата о среднем образовании
bania-bochka.ru/gde-kupit-diplom-kolledzha-v-odesse.html
Hi there everybody, here every person is sharing these kinds of familiarity, so it’s nice to read this weblog, and I used to pay
a visit this blog daily.
Window Glass Repairs It’s Not As Expensive As You
Think Repairing Glass windows
az parayla cok kazandiran slot oyunlar? az parayla cok kazandiran slot oyunlar? en kazancl? slot oyunlar?
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
Very good article. I certainly love this site. Keep writing!
Узнайте, как приобрести диплом о высшем образовании без рисков
view and download instagram stories https://storyinst.com/ .
Если хотите начать с преимуществом, активируйте промокод на лаки джет.
allows you to view instagram http://storyinst.com .
Why Car Accident Lawyers Near Me Is Everywhere This Year car
crash attorney – https://king-Wifi.win/Wiki/glennbergmann5362,
Thanks for one’s marvelous posting! I definitely enjoyed reading it, you
could be a great author.I will ensure that I bookmark your blog and will often come back from now on. I want
to encourage continue your great job, have a nice morning!
Peculiar article, just what I wanted to find.