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
стоимость 1с бухгалтерия стоимость 1с бухгалтерия .
Can you tell us more about this? I’d like to
find out more details.
I love your blog.. very nice colors & theme. Did you
make this website yourself or did you hire someone to do it for you?
Plz respond as I’m looking to construct my own blog and would like
to know where u got this from. thanks a lot
tor markets links darknet market list deep web drug store
1 win poker скачать на русском
1 win poker на андроид
Piece of writing writing is also a fun, if you be familiar
with afterward you can write or else it is complicated to write.
Feel free to surf to my web site … bulk dvds for sale
вавада казино: vavada kazi – вавада казино онлайн
Как получить диплом техникума с упрощенным обучением в Москве официально
Heya are using WordPress for your site platform?
I’m new to the blog world but I’m trying to get started and set up my own. Do you require any coding expertise to make your own blog?
Any help would be greatly appreciated!
пинап казино: пин ап кз – пин ап кз
http://vavada-kazi.ru/# vavada kazi
I enjoyed this post. For additional info, visit Acupuncture Austin
Windows that sparkle can change the whole atmosphere of a house; I’ll definitely keep your tips h Residential Roof Cleaning
http://vavada-kazi.ru/# vavada kazi
Medicines information sheet. Effects of Drug Abuse.
metformin more drug uses
All information about pills. Get information now.
I absolutely love your blog.. Pleasant colors & theme.
Did you build this amazing site yourself? Please reply back as
I’m attempting to create my very own blog and want to learn where you got this
from or just what the theme is named. Cheers! https://Ecommk.com/question/top-paint-brands-in-canada-choosing-the-best-paints-for-your-projects-4/
пин ап вход: pinup kazi – пин ап казино официальный сайт
Как правильно приобрести диплом колледжа или ПТУ в России, важные моменты
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
Late Night Fun 하이오피사이트
вавада: вавада казино зеркало – vavada kazi
Howdy! This article couldn’t be written any better!
Reading through this article reminds me of my previous roommate!
He continually kept preaching about this. I am going to forward this information to him.
Pretty sure he’ll have a very good read. Many thanks for sharing!
Have a look at my site dvd distributors
вавада онлайн казино: казино вавада – вавада казино онлайн
Howdy! I could have sworn I’ve visited your blog before but after browsing through many of the articles I
realized it’s new to me. Anyways, I’m certainly happy I discovered it and I’ll be bookmarking it and checking back often!
Как вы относитесь к влиянию социальных сетей на SEO? Интересно ваше мнение по этому вопросу? seo оптимизация сайта
вавада онлайн казино: вавада казино – вавада
I appreciated this article. For more, visit tire change at home
пинап казино: пинап казино – пин ап казино
cheap celebrex without prescription
You should be a part of a contest for one of the greatest sites online.
I am going to highly recommend this web site!