1. 删除数组中指定元素
Array.prototype.remove = function(val) {
let index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
使用
var list = ["a", "b", "c"]
list.remove("b")
2. 数组去重
Array.prototype.disrepeat = function () {
let result = this.filter((item, index) => {
return this.indexOf(item) === index;
});
return result;
};
使用
var list = ["a", "b", "c", "c","b", "a"]
var result = list.disrepeat();
3. 搜索关键字高亮
String.prototype.brightenKeyword = function brightenKeyword(keyword, font_color = "#F25D8E") {
const Reg = new RegExp(keyword, "g")
if(this) {
return this.replace(Reg, `<span style="color: ${
font_color}; font-weight: 700;">${
keyword}</span>`);
}
}
使用
"日暮苍山远,天寒白屋贫。".brightenKeyword("远")
你经常使用的 Javascript 常用方法是什么呢,请在评论区告诉我吧