前端-温故而知新-代码片段-持续更新

231 阅读2分钟

//调试技巧一安装nodejs 使用node命令执行当前的js
/**
 * @author: 吴文周
 * @name: 默认名称
 * @description: 判断数组中是否每一项都符合条件
 * @param {array,function}: 默认参数
 * @return {Boolean}: 默认类型
 * @example: 示例isAccord(array, compare)
 */
// 使用说明
const array = [1, 2, 3, 4];
console.log(
  array.every(item => {
    return item > 0;
  })
);
//业务场景回掉函数
//@param {value}: 循环数组中的每个子
//@param {index}: 循环数组中的索引(可选)
//@param {arr}: 当前数组(可选)
function compare(value, index, arr) {
  return value > 1;
}
// 业务封装
function isAccord(array, compare) {
  console.log(array.every(compare));
  return array.every(compare);
}
//调用
isAccord(array, compare);

/**
 * @author: 吴文周
 * @name: 默认名称
 * @description: 判断数组中是否有一项符合条件
 * @param {array,function}: 默认参数
 * @return {Boolean}: 默认类型
 * @example: 示例isAccord(array, compare)
 */
// 使用说明
var array = [1, 2, 3, 4];
console.log(
  array.some(item => {
    return item > 0;
  })
);
//业务场景回掉函数
//@param {value}: 循环数组中的每个子
//@param {index}: 循环数组中的索引(可选)
//@param {arr}: 当前数组(可选)
function compare(value, index, arr) {
  return value > 1;
}
// 业务封装
function isAccord(array, compare) {
  console.log(array.some(compare));
  return array.some(compare);
}
//调用
isAccord(array, compare);

/**
 * @author: 吴文周
 * @name: 默认名称
 * @description: 数组去重方方法,
 * 数组本身大小与数据结构都会对效率有不同影响,同一台机器不同时间状态都有不同的执行结果
 * @param {array,function}: 默认参数
 * @return {Boolean}: 默认类型
 * @example: 示例isAccord(array, compare)
 */
var arry = [];
for (let i = 0; i < 1000000; i++) {
  let item = parseInt(Math.random() * 10);
  arry.push(item);
}
console.time('set');
// var set = Array.from(new Set([...arry]));
var set = [...new Set([...arry])];
console.timeEnd('set');
console.log(set);

console.time('filter');
var filter = arry.filter((value, index, arr) => arr.indexOf(value) === index);
console.timeEnd('filter');
console.log(filter);

console.time('objArry');
var obj = {};
var objArry = [];
for (let item of arry) {
  if (!obj[item]) {
    obj[item] = 1;
    objArry.push(item);
  }
}
console.timeEnd('objArry');
console.log(objArry);

console.time('objArryOther');
var objOther = {};
var objArryOther = [];
var length = arry.length;
for (let i = 0; i < length; i++) {
  var item = arry[i];
  if (!objOther[item]) {
    objOther[item] = 1;
    objArryOther.push(item);
  }
}
console.timeEnd('objArryOther');
console.log(objArryOther);
/**
 * @author: 吴文周
 * @name: dateFormat
 * @description: 时间格式化方法
 * @param {String|Number|Date,String}: 默认参数
 * @return {String}: 默认类型
 * @example: 示例dateFormat('2019-11-01T10:44:57.000+0000', 'YYYY-mm-dd HH:MM:SS') 
 */
function dateFormat(time, fmt) {
  let date;
  if (typeof time === 'object') {
    date = time;
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  let ret;
  let opt = {
    'Y+': date.getFullYear().toString(), // 年
    'm+': (date.getMonth() + 1).toString(), // 月
    'd+': date.getDate().toString(), // 日
    'H+': date.getHours().toString(), // 时
    'M+': date.getMinutes().toString(), // 分
    'S+': date.getSeconds().toString() // 秒
    // 有其他格式化字符需求可以继续添加,必须转化成字符串
  };
  for (let k in opt) {
    ret = new RegExp('(' + k + ')').exec(fmt);
    if (ret) {
      fmt = fmt.replace(
        ret[1],
        ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
      );
    }
  }
  return fmt;
}