Js查漏补缺10-数组、栈、队列、回调函数等
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数组与栈</title> </head> <body> <script> arr2=[1,2,3]; document.write(arr2); <!--数组本质上是一个Object对象--> var arr1=new Array(1,2,3); document.write(typeof arr1) document.write(arr1[0]); document.write(arr1.length) <!--多维数组--> var arr3=[[1,2,3],[4,5,6]]; arr3[2]=[7,8,9]; document.write(arr3) document.write(arr3[2][2]) <!--出栈入栈--> var stack_1=["wendy"]; stack_1.push("red","black"); document.write(stack_1); stack_1.pop(); document.write(stack_1+"<br>") <!--入队出队--> var queue_1=["wendy"]; queue_1.push("red","black"); document.write(queue_1); check=queue_1.shift(); //删除队列第一个元素并返回 document.write(queue_1); document.write(check+"<br>"); <!--遍历数组--> var arr5=["a","wendy","red","black"]; for(var i=0;i<arr5.length;i++) { console.log(arr5[i]); } <!--forEach回调函数--> arr5.forEach(function(value,index,arr){ document.write(value+"<br>"); document.write(index+"<br>"); document.write(arr+"<br>"); }) <!--sort方法--> //遍历数组 var sort_arr=arr5.sort(); document.write(sort_arr+"<br>"); //带回调函数参数的话可以控制升序降序排列,貌似只能针对数字数组 //对字符数组不起作用,默认升序 var num_arr=[1,2,3] var sort_arr1=num_arr.sort(function(a,b){ return a-b;//升序 //return b-a;//降序 }); console.log(sort_arr1); </script> </body> </html>