【GET新技能】ES6的使用

ES6也成ES2015,是目前JavaScript的新标准;

  1. 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]
  1. forEach 数组循环
let arr = [2,3,2,5,6,3];
let list_el = '
    ' arr.forEach((value,index,self)=>{ list_el += `
  • ${value},${index}
  • `; //value 数组中的值 //index 数组的索引 //self 数组本身 }); list_el += '
'; document.body.innerHTML = list_el;

输出结果

2,0
3,1
2,2
5,3
6,4
3,5
  1. filter 数组过滤
let arr = [2,3,2,5,6,3];
let newArr = arr.filter((n)=>{
    return n != 2
});
alert(newArr);

输出结果:3,5,6,3

  1. 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}
  1. ....,es6扩展运算符
    
var arr = [1,2,3];
var arr2 = [...arr,4,5,6];
console.log(arr2); //[1,2,3,4,5,6]

你可能感兴趣的:(【GET新技能】ES6的使用)