目录
删除指定对象中的某个字段
delete item.childList
根据数组对象指定属性去重
const resArr = new Map();
const newArr= res.filter((a) => !resArr.has(a.platform) && resArr.set(a.platform, 1))
console.log("newArr",newArr)
过滤标签和空格
str.replace(/(<\/?[^>]*>)/g, "").replace(/ /g, '')
监听滚动条事件
componentDidMount() {
window.addEventListener('scroll', this.bindHandleScroll)
}
bindHandleScroll=(event)=> {
let scrollTop = document.documentElement.scrollTop; //滚动条滚动高度
if (scrollTop > 400) {
document.getElementById("left-top") && document.getElementById("left-top").setAttribute("style", "top:60px;position:fixed;");
} else {
document.getElementById("left-top") && document.getElementById("left-top").setAttribute("style", "top:0;position:relative;");
}
es6字符串中使用循环之后默认逗号解决
${
item.specialNewsList.length>0?item.specialNewsList.map((todo,k)=>{
return(
`<div class="li-title"><a href="news-details.html?specialId=${item.specialId}&id=${todo.newsId}">
<h4><i></i>${todo.newsTitle}</h4>
<span class="time">${todo.releaseTime}</span>
</a></div>`
)
}).join(''):'<div class="empetcon"> <img class="empetImg" src="${getImg()}global_empty_data_2.png" alt=""> <p>暂无数据</p></div>'
}
不添加 join('')
时,每个循环元素之后会默认有一个逗号,
js全屏
//全屏
fullScreen() {
var element = document.documentElement;
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
}
this.isfullScreen=true
},
//退出全屏
exitFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
this.isfullScreen=false
},
根据字节限制截取字节多的显示省略号
reBytesStr (str) {
str=str.replace(/<\/?.+?>/g,"").replace(/ /g,"").replace(/&(\S*)?;/g,"")
if((!str && typeof(str) != 'undefined')) {
return '';
}
var num = 0;
var str1 = str;
var str = '';
for(var i = 0, lens = str1.length; i < lens; i++) {
num += ((str1.charCodeAt(i) > 255) ? 2 : 1);
if(num > 6) {
break;
} else {
str = str1.substring(0, i + 1);
}
}
if(num>6){
return str+"……";
}else{
return str
}
}
使用
{this.reBytesStr(item.subjectName)}