>[info] 解析url参数字符串中的参数
```
/**
* 解析url参数字符串
* @param {将要解析的url参数字符串} str
*/
function getSearchQuerys(str) {
if (!str) return;
let arrStrs = decodeURIComponent(str).split('&');
let objStrs = {};
for(let i = 0, len = arrStrs.length; i < len; i++) {
const [ key, value ] = arrStrs[i].split('=');
if (key !== undefined && key !== '') {
objStrs[key] = value;
}
}
return objStrs;
}
```
~~~
// Example
const scenes = getSearchQuerys('a%3D1%26b%3D23ert554fr%26c%3D65gttrg');
console.log(scenes); // {a: "1",b: "23ert554fr",c: "65gttrg"}
~~~
*****
>[info] 函数去抖
```
function debance(fn, delay) {
let t = null;
return function() {
let that = this;
let args = arguments;
clearTimeout(t);
t = setTimeout(function() {
fn.apply(context, args);
}, delay);
}
}
```
~~~
// Example
let n = 1;
function func () {
n += 1;
console.log('n', n);
}
window.onresize = debance(func, 1000);
~~~
*****
>[info] 函数节流
```
function throttle (fn, delay) {
let preTime = Date.now();
return function() {
let that = this;
let args = arguments;
let nowTime = Date.now();
if (preTime + delay < nowTime) {
fn.apply(that, args);
preTime = nowTime;
}
}
}
```
~~~
// Example
let n = 1;
function func () {
n += 1;
console.log('n', n);
}
window.onresize = throttle(func, 1000);
~~~
*****
>[info] 过滤对象中的值为空的字段
```
/**
* 过滤对象中的值为空的字段,常用于前后端接口交互的数据格式处理
* @param {*待过滤的数据对象} params
*/
function dataFilters (params = {}) {
for (let key in params) {
if (params[key] === '') {
delete params[key];
}
}
return params;
}
```
*****
>[info] 数组转对象
```
/**
* 将对象数组转换为以指定字段为key的对象
* @param {Array} arrs (将要转换的数组)
* @param {String} key (以哪个字段作为对象的key)
*/
export const arrayToObj = (arrs = [], key = 'id') => {
const params = {};
for (let i = 0, len = arrs.length; i < len; i++) {
const item = arrs[i];
params[item[key]] = item;
}
return params;
};
```
*****
>[info] 对象转数组
```
/**
* 将对象转换为以key对应的值为内容的数组
* @param {Object} obj (将要转换的对象)
*/
export const objToArray = (obj = {}) => {
const arr = [];
Object.keys(obj).forEach(key => {
arr.push(obj[key]);
});
return arr;
};
```