<强>对象或数组的深拷贝强>
/* * *对象或数组的深拷贝 * @param {*} cloneObj被克隆的对象 * @param {*} targetObj克隆的目标对象 * @param {*} isOverride若属性重复,是否覆盖被克隆对象的属性 */函数deepClone (cloneObj、targetObj isOverride=true) { const _toString=Object.prototype.toString 如果(_toString.call (cloneObj) !==(对象数组)的,,_toString.call (cloneObj) !==(对象对象)){ 返回cloneObj } var cloneTarget=_toString.call (cloneObj)==='[对象数组]& # 63;[]:{} (让关键cloneObj) { 如果(Object.prototype.hasOwnProperty。调用(cloneObj键)){ 如果(_toString.call (cloneObj(例子))===(对象数组)的| | _toString.call (cloneObj[主要])===(对象对象)){ cloneTarget(例子)=deepClone (cloneObj[主要]) 其他}{ cloneTarget[主要]=cloneObj(例子) } } } 如果(targetObj,,(_toString.call (cloneObj)===_toString.call (targetObj))) {//这里要注意,克隆的目标对象也要deepClone下 cloneTarget=isOverride & # 63;对象。分配(cloneTarget deepClone (targetObj)) :Object.assign (deepClone (targetObj)、cloneTarget) } 返回cloneTarget }
<>强精准判断数据类型强>
//精准判断数据类型 函数getVerifyDataTypes () { const类型=[“字符串”,“数量”,“布尔”、“空”、“定义”、“功能”、“对象”,“数组”,“日期”,“错误”,“正则表达式”,“象征”、“地图”、“套”) 让Type={}//示例用法:Type.isString (javascript) (让我=0;我& lt;types.length;我+ +){ 类型(“${类型[我]}')=obj=比;Object.prototype.toString.call (obj)===" ${类型[我]}][对象” }//判断字符串是否为json格式 类型。isJsonStr=str=比;{ 如果(typeof str=='字符串'){ 尝试{ 让obj=JSON.parse (str); 如果(obj,,typeof obj=='对象'){ 返回true; } 返回错误; }捕捉(e) { 返回错误; } 其他}{ 返回错误; } } 返回类型 }
<强>日期格式化强>
/* * *日期格式化 * @param{*}日期日期对象 * @param {*} beforeHyphen年月日连字符 * @param {*} afterHyphen时分秒连字符 */函数formatDate(日期=new日期(),beforeHyphen=?”, afterHyphen=': ') { const formatNumber=n=比;{ n=n.toString () 返回n [1] & # 63;n:“0 $ {n}’ } const年=date.getFullYear () const月=date.getMonth () + 1 const天=date.getDate () const小时=date.getHours () const分钟=date.getMinutes () const第二=date.getSeconds () const ymd=(年、月、日). map (formatNumber) . join (beforeHyphen) const hms=(小时,分钟,秒]. map (formatNumber) . join (afterHyphen) 返回“$ {ymd} $ {hms}” }
<强>把时间戳转换为剩余的天,时,分,秒强>
/* * *把时间戳转换为剩余的天,时,分,秒,一般应用于倒计时场景中 * @param{*}时间戳时间戳 */函数converTimestamp(时间戳){ const formatNumber=n=比;{ n=n.toString () 返回n [1] & # 63;n:“0 $ {n}’ } 让天=数学。地板(时间戳/1000/3600/24); 让小时=数学。地板(24)(时间戳/1000/3600)%; 让分钟=数学。地板((时间戳/1000/60)% 60); 让第二=数学。地板(时间戳/60 1000%); 返回{ 一天,一天, 小时:formatNumber(小时) 分钟:formatNumber(分钟), 第二:formatNumber(第二次) } }
<强>从数组中随机取出一项强>
//从数组中随机取出一项 函数getRandomItemByArray(物品){ 返回项目(Math.floor (math . random () * items.length)]; }
<强>将有父子关系的数组转换成树形结构数据强>
让数据=https://www.yisu.com/zixun/[ {parentId: 0, id: 1、价值:“xxx”}, {parentId: 1、id: 3,价值:“xxx”}, {parentId: 4、id: 6、价值:“xxx”}, {parentId: 3、id: 5、价值:“xxx”}, {parentId: 2、id: 4、价值:“xxx”}, {parentId: 1、id: 2,价值:“xxx”}, ]//转换为树形阵列结构 函数toTreeAry(加勒比海盗,pId=0) { 返回加勒比海盗 .filter (({parentId})=比;parentId===pId) . map(=比;({ 一个, 孩子们:toTreeAry(加勒比海盗。过滤器(({parentId})=比;parentId !==pId), a.id) })) }//转换为树形对象结构 函数toTreeObj(加勒比海盗,pId=0) { 让res={} 加勒比海盗。过滤器(({parentId})=比;parentId===pId) .forEach(=比;{ res (a。id]={ 一个, 孩子们:toTreeObj(加勒比海盗。过滤器(({parentId})=比;parentId !==pId), a.id) } }) 返回res } console.log (toTreeAry(数据) console.log (toTreeObj(数据)JavaScript常用工具函数库汇总