ES6个人笔记记录——数组解构

function* fibs(){
	let a = 0;
	let b = 1;
	while(1){
		yield a;
		[a,b] = [b, a + b];
	}
}

let[first,second,third,fourth,fifth,sixth] = fibs();
console.log(sixth);

let [a,b] = [3,4];
console.log(typeof b);

let {log,sin,cos,tan} = Math;
console.log(tan(10));

// 函数参数的解构赋值
let add = ([a,b]) => {
	return a + b;
}
console.log(add([2,3]));

// 冒泡
let balloon = ({arr}) => {
	let length = arr.length;
	for(let i = 0;i < length;i++){
		for(let j = i + 1; j < length; j++){
			if(arr[i] > arr[j]){
				// 解构
				[arr[i],arr[j]] = [arr[j],arr[i]];
			}
		}
	}
	return arr;
}

let arr = [10,19,1,4,2,7,9]
console.log(balloon({arr}))

// 提取JSON数据
let jsonData = {
	id : 42,
	status : "OK",
	data : [867,5309]
}

let {id,status,data} = jsonData;
console.log(id,status,data);
console.log(typeof data);

// map [key] [,value]

你可能感兴趣的:(javascript,前端学习)