常用方法封装

常用方法封装

修改对象的key值
1
2
3
4
5
6
7
8
9
10
11
12
//如果data是对象可以免去遍历这一步
replaceKey(data, oldKeyName, newKeyName) {
data.forEach(item => {
for (let key in item) {
if (key == oldKeyName) {
item[newKeyName] = item[key];
delete item[key];
}
}
});
return data;
}
es6解构赋值删除不需要的属性
1
2
let {phone, qqList, ...obj} = {ck1: '1', phone:"10086", qqList:{}, ck2: '2', ck3: '3'};
console.log(obj); // {ck1: '1', ck2: '2', ck3: '3'}
reduce删除对象中重复的属性–网上的例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let person = [
{id: 0, name: "小明"},
{id: 1, name: "小张"},
{id: 2, name: "小李"},
{id: 3, name: "小孙"},
{id: 1, name: "小周"},
{id: 2, name: "小陈"},
];

let obj = {};

person = person.reduce((cur,next) => {
obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
return cur;
},[]) //设置cur默认类型为数组,并且初始值为空的数组
//console.log(person);
reduce数组合并去重并修改数组中对象的属性值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
let arr = [{ name: '应继武', vendor: 1 }, { name: '李四', vendor: 1 },{ name: '张三', vendor: 1 }];
let arr1 = [{ name: '应继武', vendor: 2 }, { name: '张三', vendor: 2 }, { name: '王五', vendor: 2 }];
let list = [...arr, ...arr1];

let d = []
let hash = {}
d = list.reduce((item, next, index, sArr) => {
// console.log(item, next, index, sArr)
hash[next.name] ? item=setArr(next.name,item) : hash[next.name] = true && item.push(next)
return item
}, [])

function setArr(name,list){
list.forEach(item=>{
if(item.name===name){
item.vendor=3
}
})
return list
}
//console.log(d)
//[{ name: '应继武', vendor: 3 }, { name: '李四', vendor: 1 },{ name: '张三', vendor: 3 }, { name: '王五', vendor: 2 }]
检测是否是数组
1
2
3
4
Array.isArray([]);  // true
Array.isArray(undefined); // false;

array instanceof Array; // true 检测对象的原型链是否指向构造函数的prototype对象
数组的一些操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//遍历数组
let arr=[1,2,3,4,3,2,1];
//forEach遍历数组的话,不能用break跳出循环,也不能用return返回外层。
//for in遍历的是数组的索引(即键名),而for of遍历的是数组元素值
for (let value of arr) {//break 跳出循环下面的方法继续执行;return 终止运行
console.log(value);//输出1,2,3,4,3,2,1
}

//map 返回一个新数组
let users = [
{name: "张含韵", "email": "zhang@email.com"},
{name: "江一燕", "email": "jiang@email.com"},
{name: "李小璐", "email": "li@email.com"}
];
let emails = users.map(item=>item.email);

//filter 返回过滤后的新数组
let arr=[1,2,3,4,3,2,1,2];
let newArr1=arr.filter((value,index,arr)=>value>=3)
console.log(newArr1);//输出[3,4,3]
时间戳转 y-m-d h:m:s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function timestampToTime(timestamp,full) {
var date = timestamp.length == 10 ? new Date(timestamp * 1000) : new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
Y = date.getFullYear() + '-';
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ':';
s = date.getSeconds();
//return Y+M+D+h+m+s;
if (full) {
return Y + M + D + h + m + s;//2018-11-11 11:11:11
} else {
return { ymd: Y + M + D, hms: h + m + s }
}
}
判断一个变量是不是空对象
1
2
3
function isEmptyObj(obj) {
return obj !== null && typeof obj === 'object' && !Array.isArray(obj) && (Object.getOwnPropertyNames(obj).length === 0) && (Object.getOwnPropertySymbols(obj).length === 0)
}
分割一位数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function cutArr(size, arr) {
var result = [];
for (var i = 0; i < arr.length; i += size) {
result.push(data.slice(i, i + size));
}
return result
}
//示例
var data = [
{name:'Liming',age:'25'},
{name:'Liming',age:'25'},
{name:'Liming',age:'25'},
{name:'Liming',age:'25'},
{name:'Liming',age:'25'},
{name:'Liming',age:'25'},
{name:'Liming',age:'25'},
{name:'Liming',age:'25'},
{name:'Liming',age:'25'},
{name:'Liming',age:'25'},
];
cutArr(3,data)

jq判断div是否滚动到底部(loadMore)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$('#div').scroll(function (event) {
var scrollTop = $(this).scrollTop();//表示滚动条滚动的高度
var height = $(this).height();//div高度
var scrollHeight = $(this)[0].scrollHeight;//表示滚动条需要滚动的高度
// var scrollTop = event.currentTarget.scrollTop;
// var height = event.currentTarget.clientHeight;
// var scrollHeight = event.currentTarget.scrollHeight;
//console.log(scrollTop, height, scrollHeight);
if (scrollTop + height == scrollHeight) {
console.log('到底了');
}
if (scrollTop == 0) {
console.log('到顶了');
}
})
shadow wechat
扫一扫,关注微信公众号。