时间格式化

  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  9. }

个位数前面填充一个0

  1. const formatNumber = n => {
  2. n = n.toString()
  3. return n[1] ? n : '0' + n
  4. }

根据下标删除数组元素

  1. const removeItem = (arr, index) => {
  2. arr.splice(index, 1)
  3. }

从数组中删除指定的一个元素

  1. const removeItem = (arr, val) => {
  2. var index = arr.indexOf(val);
  3. if (index > -1) {
  4. arr.splice(index, 1);
  5. }
  6. }

从数组中删除指定的多个元素

  1. const arrRemoves = (arr, val) => {
  2. var newIndexs = val.map(function(val, idx){return val - idx})
  3. newIndexs.forEach(function(index){
  4. arr.splice(index, 1)
  5. })
  6. }

数组根据对象属性排序

  1. var arr=[{name:"rose", id:3},{name:"jack"id:1},{name:"mafy"id:2}]
  2. function rule(key) {
  3. return function (a, b) {
  4. return a[key] - b[key]
  5. }
  6. }
  7. arr.sort(rule("id"))

随机打乱数组顺序

  1. var arr = [1, 2, 3, 4, 5, 6];
  2. function randomsort(a, b) {
  3. return Math.random() > .5 ? -1 : 1
  4. }
  5. arr.sort(randomsort)

手机号码加*号

  1. var phone = 13834236433;
  2. function addStar(phone) {
  3. return phone.substr(0, 3) + '****' + phone.substr(-4, 4);
  4. }
  5. addStar(phone);

根据数组对象元素id去重

  1. function uniarr (arr) {
  2. var hash = [];
  3. for (var i = 0; i < arr.length; i++) {
  4. for (var j = i + 1; j < arr.length; j++) {
  5. if (arr[i].id == arr[j].id) {
  6. ++i;
  7. }
  8. }
  9. hash.push(arr[i]);
  10. }
  11. return hash;
  12. }

重复字符串N次

  1. var times = function(str, num) {
  2. return num > 1 ? str += times(str, --num) : str;
  3. }

js 实现复制

  1. <textarea id="txt">hello world</textarea>
  2. <script type="text/javascript">
  3. window.onload = function() {
  4. var Url2=document.getElementById("txt");
  5. Url2.select(); // 选择对象
  6. document.execCommand("Copy"); // 执行浏览器复制命令
  7. console.log("已复制好,可贴粘。");
  8. }
  9. </script>

图片流插件

[https://github.com/desandro/masonry](https://github.com/desandro/masonry)

图片轮播图插件

  • swiper.js
  • 轮播图 [https://github.com/stevenwanderski/bxslider-4](https://github.com/stevenwanderski/bxslider-4)

浏览器拷贝粘贴插件

[拷贝粘贴] (https://github.com/zenorocha/clipboard.js)[https://github.com/zenorocha/clipboard.js](https://github.com/zenorocha/clipboard.js)

在浏览器中打开微信

<a herf="weixin://"></a>

日期选择器

日期选择器 [https://github.com/ddd702/datePicker](https://github.com/ddd702/datePicker)

地址选择器

地址选择器 [https://github.com/nzbin/Framework7-CityPicker](https://github.com/nzbin/Framework7-CityPicker)