不要吹灭你的灵感和你的想象力; 不要成为你的模型的奴隶。 ——文森特・梵高
正则使用
// inputVal 用户输入的内容
// val 目前页面显示的内容(输入之前的内容)
let reg = /\([\w\W]*?\)|([\w\W]*?)/g // 兼容中英文两种括号
let input = inputVal.replace(reg, '') // 去掉现在在输入的内容括号里边的空格
let value = val && val.replace(reg, '') // 去掉括号里边原来的内容的空格
// 比对去掉空格内容后,是否一致,如果是一致的话,
// 则说明没有修改空格以外的内容,则让用户修改
if (input === value) {
form[key] = inputVal // 数据更新成用户输入的内容 }
数组去重
quZhong(arr) { return [...new Set(arr)] }
判断值的数据类型
type(para) {
return Object.prototype.toString.call(para)
}
forEach跳出循环
forEach是不能通过break或者return来实现跳出循环的,为什么呢?实现过forEach的同学应该都知道,forEach的的回调函数形成了一个作用域,在里面使用return并不会跳出,只会被当做continue
getItemById(arr, id) {
var item = null;
try {
arr.forEach(function(curItem, i) {
if (curItem.id == id) {
item = curItem;
throw Error();
}
console.log(curItem,'查看跳出循环')
});
} catch (e) {}
return item;
}
includes使用
includes()的作用,是查找一个值在不在数组里,若在,则返回true,反之返回false。 基本用法:
let arr = ['a', 'b', 'c'];
let parms = 'a'
arr.includes(parms) // true
将页面重定向到另一个页面
window.location.href='';
window.location.replace='';
判断数组
Array.isArray
let arr = [{id:'1'}]
console.log(Array.isArray(arr),'判断')
递归函数
方法
// 获取树的key
deleNullSon(node) {
if(node.children != undefined) {
node.children.length == 0 ? delete node.children : '';
}
if (node.children && node.children.length > 0) {
for (let a = 0; a < node.children.length; a++) {
this.deleNullSon(node.children[a]);
}
}
},
使用
for (let i = 0; i < res.result.length; i++) {
let temp = res.result[i];
this.$daTools.deleNullSon(temp)
}