1. 程式人生 > >es6陣列新增2

es6陣列新增2

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>陣列擴充套件運算子</title>
	</head>
	<ul id='#oUl'>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
	</ul>
	<body>
		<script type="text/javascript">
			let arr = [1,2,3];
			let arr2 = [...arr];  //...
			console.log(arr);
			console.log(arr2);
			
			
			let strName = 'jiangfei';//類陣列:只要具備length就比較靠譜  json在後面必須加入length
			console.log([...strName]);
			console.log([].slice.call(strName));
			console.log(Array.from(strName));
			
			let json = {
				0:"apple",
				1:"banner",
				2:"orange"
			}
			let json2 = {
				0:"apple",
				1:"banner",
				2:"orange",
				length:3
			}
			let json3 = {
				0:"apple",
				1:"banner",
				2:"orange",
				length:2
			}
			console.log(Array.from(json));
			console.log(Array.from(json2));
			console.log(Array.from(json3));
			console.log([].slice.call(json3));
			/*console.log([...json2]);*/
			
			
			/*
			 
			 * arr.keys()每個元素的索引   arr.entriers()所有元素包括索引     ES6陣列的特性
			 * */
			
			
			let oUl = document.querySelectorAll('ul li');//偽陣列
			let arr3 = [].slice.call(oUl);//將偽陣列轉換為陣列 es5   [].slice.call(偽陣列)
			let arr4 = Array.from(oUl);//將偽陣列轉換為陣列 es2015   Array.from(偽陣列)把類陣列(獲取一組元素、arguments...)物件轉換為陣列  
			let arr5 = [...oUl];//將偽陣列轉換為陣列 es6  [...偽陣列]
			console.log(oUl);
			console.log(arr3);
			console.log(arr4);
			console.log(arr5);
					
					
			function show(){
				console.log(arguments);
				console.log(arguments[0]);
				console.log(arguments[1]);
				console.log(arguments[2]);
				let arr6 =[...arguments];
				console.log(arr6);
				console.log(Array.from(arguments));
				console.log([].slice.call(arguments));
				console.log(arguments.length);
			}
			show(1,2,3,4,5)
			
			
			//Array.of()將一組值轉成陣列 感覺沒啥用
			 
			 let strAdd = "'sdgfsagraehdfh','sdgfsagraehdfh'";
			let arr7 = Array.of('aaaa','bbbb','cccc');
			let arr8 = Array.of(strAdd);
			console.log(arr7);
			console.log(arr8);
			
			
			//arr.find(function(val,index,arr){return});查找出第一個符合條件的陣列成員,如果沒有找到,返回undefined;
			
			let arr9 = [23,12,101,199];
			
			let res = arr9.find((val,index,arr9)=>{
				return val>100;
			});
			console.log(res);
			
			let arrVal = arr9.find(function(val,index,arr9){
				return val>100;
			})
			console.log(arrVal);
		
		//arr9.findIndex((val,index,arr9)=>{return val>100;})  查找出第一個符合條件的陣列成員,並返回他的位置,如果沒有找到,返回undefined;
		let arrIndex = arr9.findIndex((val,index,arr9)=>{
			return val>100;
		})
		console.log(arrIndex);
		
		//arr.fill(填充的東西,開始位置,結束位置) 包括起始位置,不包括結束位置
		
		let arr10 =new Array(10);
		
		
		arr10.fill('預設值',2,5);
		console.log(arr10)
		
		//arr.includes()判斷陣列中是否存在指定的元素 返回值為ture/false
		console.log(arr.includes(1));
		
		
		
		</script>
	</body>
</html>