zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    learnyou 相关网站
    hdu 3038 How Many Answers Are Wrong
    hdu 3047 Zjnu Stadium 并查集高级应用
    poj 1703 Find them, Catch them
    poj 1182 食物链 (带关系的并查集)
    hdu 1233 还是畅通工程
    hdu 1325 Is It A Tree?
    hdu 1856 More is better
    hdu 1272 小希的迷宫
    POJ – 2524 Ubiquitous Religions
  • 原文地址:https://www.cnblogs.com/cuijunfeng/p/13167118.html
Copyright © 2011-2022 走看看