日常操作判斷

  1. es6 async方法實現(xiàn)異步傳參數(shù)
function timeout(value,ms) {
  return new Promise((resolve, reject) => {
    setTimeout(function() {
      resolve(value);
    }, ms);
  });
}
async function asyncPrint(value, ms) {
  let b = 0;
  await timeout(value,ms).then(function(a) { b = a; });
  console.log(b);
}
asyncPrint('hello world', 5000);
  1. es6刪除數(shù)組某一項
arr.splice(arr.findIndex(item => item.id === 8), 1)
  1. JSON格式判斷
const jsonParse = function(data) {
    if(typeof data == 'string') {
        try {
            return JSON.parse(data);
        } catch(e) {
            return;
        }
    }
}
  1. 序號格式化
const orderNumFn = function(num, len) {
    return (Array(len).join('0') + num).slice(-len);
}
  1. 深度復制對象
const cloneObj = function(obj) {
  if(obj!= null && typeof obj == 'object') {
    return JSON.parse(JSON.stringify(obj));
  }
}
  1. 獲取數(shù)據(jù)類型(通用包括Object,Array,Null,undefined)
const getType = function(v) {
    let type = Object.prototype.toString.call(v).toLowerCase(),
        regex = /^\[object\s+([a-z]+)\]$/;
    return type.match(regex)[1];
}
  1. 自定義遍歷方法
const customEach = function(opts,callback) {
  if(getType(opts.data) == 'array') {
    opts.data.forEach(function(item,index) {
        callback.call(this,item,index);
    });
  } else if(getType(opts.data) == 'object') {
    for(let i in opts.data) {
      callback.call(this,opts.data[i],i);
    }
  }
  return opts.data;
}
  1. 格式化數(shù)據(jù)(在數(shù)量前添加'+'/'-')
const numFormat = function(v,status,type) {
    let formatStr = '';
        v = parseInt(v);
    if(v !== 0) {
        formatStr = status + v;
    }else if(v === 0) {
        formatStr = v;
    }
    return formatStr;
}
  1. 判斷內(nèi)容為空時顯示 '--'
const isEmpty = function(str,empty) {
    if(typeof str === 'undefined' || str === null || str.length < 1) {
        return empty || '----';
    } else {
        return str;
    }
}
  1. 遍歷更新刪除數(shù)組
/*
    遍歷更新刪除數(shù)據(jù)
    opts {}
    oldData    需要被更新的數(shù)據(jù)
    updateData 需要刪除更新到opts.oldData里的數(shù)據(jù)
    type       數(shù)據(jù)更新類型(如init,add,update,del)
    idNameStr  數(shù)據(jù)項的id(如point_id,fid)
*/
const eachData = function(opts) {
    let updateData = opts.updateData,
        oldData = opts.oldData,
        type = opts.type,
        idNameStr = opts.idNameStr;

    $.each(updateData,function(upIndex,upItem){
        let oldVal = '',
            oldIndex = '',
            newIndex = '';
        oldData.forEach(function(oItem,oIndex) {
            // 刪除
            if(type === 'delete' && oItem[idNameStr] === upIndex) {
                oldData.splice(oIndex,1);
            } else {
            // 更新和新增
                if(type === 'add') {
                    oldVal = upItem;
                }else if(type === 'update' && oItem[idNameStr] === upIndex) {
                    $.extend(oItem,upItem);                 
                    oldVal = oItem;
                    oldIndex = oIndex;                                                                                                                                                                                              ;
                }
                if(upItem.insert_fid_below == 0) {
                    newIndex = 0;
                } else if(oItem[idNameStr] == upItem.insert_fid_below) {
                    if(newIndex < oldData.length) {
                        newIndex = oIndex + 1;
                    } else {
                        newIndex = curIndx;
                    }
                }
            }

        });
        if(type === 'update' && oldVal !== '' && newIndex !== '') {
            if(oldIndex > newIndex) {
                oldIndex = oldIndex + 1;
            }
            oldData.splice(newIndex,0,oldVal);
            oldData.splice(oldIndex,1); 
        } else if(type === 'add' && oldVal !== '' && newIndex !== '') {
            oldData.splice(newIndex,0,oldVal)
        }

    });
}
/*
    refreshData 刷新數(shù)據(jù)
    opts {}
    oldData           需要被刷新的數(shù)據(jù)
    updateCount       需要更新的數(shù)量
    updateData        需要增刪改到opts.oldData里的數(shù)據(jù)
    idNameStr         數(shù)據(jù)項的id(如point_id,fid)
*/
const refreshData = function(opts) {
    let oldData = opts.oldData,
        updateData = opts.updateData,
        idNameStr = opts.idNameStr;
    // 賦值id
    if(opts.updateId) oldData.id = opts.updateId;

    // 賦值count
    if(opts.updateCount || opts.updateCount == 0) oldData.count = opts.updateCount; 

    // 初始化數(shù)據(jù)
    if(updateData.init) {
        oldData.data = updateData.init;
    }

    // 添加數(shù)據(jù)
    if($.isPlainObject(updateData.add)) {
        eachData({
            idNameStr : idNameStr,
            oldData : oldData.data,
            updateData : updateData.add,
            type : 'add'
        });
    }

    // 更新數(shù)據(jù)
    if(updateData.update) {
        eachData({
            idNameStr : idNameStr,
            oldData : oldData.data,
            updateData : updateData.update,
            type : 'update'
        });
    }

    // 刪除數(shù)據(jù)
    if(updateData.del) {
        eachData({
            idNameStr : idNameStr,
            oldData : oldData.data,
            updateData : updateData.del,
            type : 'delete'
        });
    }
}
  1. 表頭固定
const cellFixedFn = function(e,fixedCell,sHeight) {
    let curEle = $(e.currentTarget),
        _fixedCell = curEle.find(fixedCell),
        scrollTop = curEle[0].scrollTop;            
    _fixedCell.css({
        '-webkit-transform': 'translateY('+scrollTop+'px)',
        '-ms-transform': 'translateY('+scrollTop+'px)',
        'transform': 'translateY('+scrollTop+'px)'
    });
}
  1. 隨機id
createUuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
      return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16);
    });
},
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容