zoukankan      html  css  js  c++  java
  • JS-基础-04.Math库、数组、表

    一、Math工具函数

      1:Math.PI
      2:Math.random 返回 [0, 1)范围的数;
      
    // 随机产生 一个[a, b]之前的整数;
    function random_int (min, max) {
        var value = min + (max - min + 1) * Math.random(); // [0, max-min)--> min + [min, max + 1)
        value = Math.floor(value);
        
        return value;
    }
      3:Math.floor(); 向下取整数;
      4:Math.sin, Math.cos, Math.tan 三角函数
      
    // 三角函数, sin, cos, tan, 参数是弧度,传入一个角度--> sin;
    // 90--> Math.PI / 2, 180 Math.PI, 360 --> 2 * Math.PI
    // 45 --> Math.PI / 4; sin(45) = 根号(2) / 2 == 0.707
    value = Math.sin(Math.PI / 4);
    console.log(value);
    
    value = Math.cos(Math.PI / 4);
    console.log(value);
    
    value = Math.tan(Math.PI / 4);
    console.log(value);
    // 其他的三角函数,可以百度查到
    function rad2deg(r) {
        var degree = r * 180 / Math.PI;
        return degree;
    }
    
    
    function deg2rad(degree) {
        var r = (degree / 180) * Math.PI;
        return r;
    }
    
    value = rad2deg(Math.PI / 4)
    console.log(value);
    // end
    
    // 反三角函数,给你一个 正玄值,--> 返回一个弧度
    // []
    value = Math.sin(deg2rad(90));
    value = Math.asin(value);
    console.log(rad2deg(value));
    
    value = Math.cos(deg2rad(90));
    value = Math.acos(value);
    console.log(rad2deg(value));
    
    value = Math.tan(deg2rad(88));
    value = Math.atan(value);
    console.log(rad2deg(value));
    // end 
    
    // atan2: (-PI, PI]
    var r = Math.atan2(1, 1);
    value = rad2deg(r);
    console.log(value);
    
    r = Math.atan2(-1, -1);
    value = rad2deg(r);
    console.log(value);
      5: 角度转弧度,弧度转角度;
      6: 反三角函数Math.asin, Math.acos, Math.atan;
      7: Math.atan2(y, x), 返回一个坐标(y, x)对应的角度;(-PI, PI];
      8: Math.sqrt 开根号;
    二、数组的高级使用
      1:array.length; 获取数组的长度;
      2:遍历一个数组; for(var key in array);
    // 遍历
    for (var index in array_data) {
        console.log(array_data[index]);
    }
    
    for(var i = 0; i < array_data.length; i ++) {
        console.log(array_data[i]);
    }
      3: 向数组末尾加入一个元素; push
    // 加一个元素到数组的末尾
    array_data.push(100);
    array_data.push(200);
    array_data.push(300);
    array_data.push("Hello javascript");
    
    array_data.push({key: "value"});
      4: 查找对象在数组中所对应的索引; indexOf()
      5: 删除数组的某个元素; splice(开始索引,要删除的个数)
    var index = array_data.indexOf(500)
    console.log(index);
    
    // 开始的索引, 删除的个数,返回删除的数组, 原来的数组元素少了;
    var data2 = array_data.splice(2, 2)
    console.log(data2);
    console.log(array_data);
      6: 数组的排序;
    // 数组的排序
    array_data = [3, 7, 6, 5];
    // 比较函数, 传给这个排序的函数,排序的函数,使用这个比较函数的结果,来决定大小,来排序;
    // 指定的比较大小的方法
    array_data.sort(function(lhs, rhs) {
        if (lhs > rhs) {
            return -1; // lhs排在rhs前面
        }
        else if(lhs < rhs) {
            return 1; // rhs排在lhs前面
        }
        else {
            return 0; // lhs == rhs ;
        }
    });
    console.log(array_data);
      7: 随机 打乱一个数列;
      8:随机的从一堆的数据里面抽取一个值;
    function random_array(array_data)  {
        array_data.sort(function(lhs, rhs) { // 随机决定他们的大小
            if(Math.random() <= 0.5) {
                return -1;
            }
            else {
                return 1;
            }
        })
    }
    
    array_data = [101, 201, 301, 501, 701];
    random_array(array_data);
    console.log(array_data);
    
    random_array(array_data);
    console.log(array_data);
    
    random_array(array_data);
    console.log(array_data);
    
    
    value = array_data[0];
    console.log(value);
    三、表的高级使用
      1:遍历一个表; for(key in table)
      2: 删除表中的数据; delete list_data[4];
    // 删除掉key的函数
    delete test_table["age"];
    delete test_table.name;
    // end
    for (var key in test_table) {
        console.log(key, test_table[key]);
    }
    四、字符串对象的高级使用
      1:str.length;属性
      2: str.indexOf();返回子串首次出现的位置;
      3:str.replace(/Microsoft/,"W3School");
      4:toLowerCase, toUpperCase;
    var str = "HelloWorld";
    console.log(str.length);
    
    console.log(str.indexOf("or"));
    
    // 不会改变原来的字符串;
    var new_str = str.replace("Hello", "3q");
    console.log(str, new_str);
    
    
    new_str = str.toLowerCase();
    console.log(str, new_str);
    
    new_str = str.toUpperCase();
    console.log(str, new_str);
    
    str = str.toUpperCase(); // str 指向了这个新产生的字符对象;
    console.log(str);
  • 相关阅读:
    Java基础--day04
    Java基础--day03
    Java基础--day02
    高斯键盘设置指南
    博客园主题配置
    算法笔记--二分
    Test2反思
    树链剖分【模板】
    7.20关于莫队算法的一些初步理解
    分块(n根n复杂度)
  • 原文地址:https://www.cnblogs.com/orxx/p/10391868.html
Copyright © 2011-2022 走看看