1、数组去重
var arr = [1, 2, 3, 3, 4, 4];
console.log(Array.from(new Set(arr)));
console.log(...new Set(arr)); // 简化
2、合并对象
const page = {
current: 1,
pageSize: 10
}
const form = {
name: "",
sex: ""
}
const params = { ...form, ...page };
console.log(params);
/*{
name: "",
sex: "",
current: 1,
pageSize: 10
}*/
3、获取查询参数
// 假设url后面是 ?post=123&name=lzq
// 正则的方式来完成获取查询参数
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
return r ? r[2] : null;
}
console.log(getQueryString('name'));
console.log(getQueryString('post'));
// ES6 URLSearchParams获取参数
var urlParams = new URLSearchParams(window.location.search);
console.log(urlParams.has('post')); // true
console.log(urlParams.get('name')); // "lzq"
console.log(urlParams.getAll('name')); // ["lzq"]
console.log(urlParams.toString()); // "?post=123&name=lzq
console.log(urlParams.append('active', '1')); // "?post=123&name=lzq&active=1"