数组连接
let arr1 = [1,2,3]let arr2 = [4,5,6]//方法一:arr1.push.apply(arr1,arr2);//方法二:arr1 = arr1.concat(arr2)//方法三:arr1.push(...arr2)
map()遍历
let arr = [{'a':1},{'b':2}]arr = arr.map((item)=>{return {...item,isshow:false}})//[{'a':1,isshow: false},{'b':2, isshow: false}]
for…in/for…of
- 使用for in会遍历数组所有的可枚举属性,包括原型。
for in遍历的是数组的索引(即键名),而for of遍历的是数组元素值。
Array.prototype.method=function(){console.log(this.length);}var myArray=[1,2,4,5,6,7]myArray.name="数组"for (var index in myArray) {console.log(myArray[index]);//1,2,4,5,6,7,数组,f(){console.log(this.length);}}
for..of适用遍历数/数组对象/字符串/map/set等拥有迭代器对象的集合.但是不能遍历对象,因为没有迭代器对象.与forEach()不同的是,它可以正确响应break、continue和return语句
- for-of循环不支持普通对象,但如果你想迭代一个对象的属性,你可以用for-in循环(这也是它的本职工作)或内建的Object.keys()方法:
