ES6也成ES2015,是目前JavaScript的新标准;
- new Set() 数组去重
let arr = [2,3,2,5,6,3];
let set = new Set(arr);
let newArr = Array.from(set);
alert(newArr);//[2,3,5,6]
- forEach 数组循环
let arr = [2,3,2,5,6,3];
let list_el = '<ul>'
arr.forEach((value,index,self)=>{
list_el += `<li>${value},${index}</li>`;
//value 数组中的值
//index 数组的索引
//self 数组本身
});
list_el += '</ul>';
document.body.innerHTML = list_el;
输出结果
2,0
3,1
2,2
5,3
6,4
3,5
- filter 数组过滤
let arr = [2,3,2,5,6,3];
let newArr = arr.filter((n)=>{
return n != 2
});
alert(newArr);
输出结果:3,5,6,3
- Object.assign() 对象合并方法 避免引用发生
let json1 = {a:1};
let json2 = {b:2};
let json3 = Object.assign({},json1,json2);
console.log(json3);//{a: 1, b: 2}
console.log(json1);//{a:1}
console.log(json2);//{b:2}
....,es6扩展运算符
var arr = [1,2,3];
var arr2 = [...arr,4,5,6];
console.log(arr2); //[1,2,3,4,5,6]
网友评论