function unique1(arr) {
return [...new Set(arr)]
}
function unique2(arr) {
var obj = {};
return arr.filter(ele => {
if (!obj[ele]) {
obj[ele] = true;
return true;
}
})
}
function unique3(arr) {
var result = [];
arr.forEach(ele => {
if (result.indexOf(ele) == -1) {
result.push(ele)
}
})
return result;
}
字符串去重
String.prototype.unique = function () {
var obj = {},
str = '',
len = this.length;
for (var i = 0; i < len; i++) {
if (!obj[this[i]]) {
str += this[i];
obj[this[i]] = true;
}
}
return str;
}
###### //去除连续的字符串
function uniq(str) {
return str.replace(/(\w)\1+/g, '$1')
}
判断元素有没有子元素
function hasChildren(e){
var children = e.childNodes,
len = children.length;for(var i = 0; i < len; i++){if(children[i].nodeType === 1){returntrue;}}returnfalse;}
原生js封装ajax
function ajax(method, url, callback, data, flag){
var xhr;
flag = flag ||true;
method = method.toUpperCase();if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();}else{
xhr = new ActiveXObject('Microsoft.XMLHttp');}
xhr.onreadystatechange =function(){if(xhr.readyState == 4 && xhr.status == 200){
console.log(2)
callback(xhr.responseText);}}if(method =='GET'){
var date= new Date(),
timer = date.getTime();
xhr.open('GET', url + '?' + data + '&timer' + timer, flag);
xhr.send()}elseif(method =='POST'){
xhr.open('POST', url, flag);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(data);}}