zoukankan      html  css  js  c++  java
  • Javascript备忘复习笔记1

    一.字符串操作

    1.大小写

    var s = "hello";
    undefined
    g = s.toUpperCase();
    "HELLO"
    g;
    "HELLO"
    g.toLowerCase();
    "hello"
    View Code

    2.索引/截断

    s.indexOf('o');
    4
    all.js:1 loading comments...
    var l = "hello world";
    undefined
    l.substring(0,5);
    "hello"
    l.substring(7);
    "orld"
    View Code

    二.数组

    1.索引/长度

    var arr = [1, 2, 3.14, 'Hello', null, true];
    undefined
    arr.length;
    6
    arr[2]=99;
    99
    arr;
    [1, 2, 99, "Hello", null, true]
    View Code

    2.切片

    arr;
    [1, 2, 99, "Hello", null, true]
    arr.slice(1,3);
    [2, 99]
    View Code

    3.pop/push与shift/unshift(从头删除,从头插入)

    arr;
    [1, 2, 99, "Hello", null, true]
    arr.pop();
    true
    arr;
    [1, 2, 99, "Hello", null]
    arr.push(1,"sd");
    7
    arr;
    [1, 2, 99, "Hello", null, 1, "sd"]
    arr.unshift('head','sec');
    9
    arr;
    ["head", "sec", 1, 2, 99, "Hello", null, 1, "sd"]
    arr.shift();
    "head"
    arr;
    ["sec", 1, 2, 99, "Hello", null, 1, "sd"]
    View Code

    4.排序/翻转/指定索引删除

    var arr = [1, 2, 3.14, 'Hello', null, true];
    undefined
    arr.sort();
    [1, 2, 3.14, "Hello", null, true]
    arr.reverse();
    [true, null, "Hello", 3.14, 2, 1]
    var arr = ['Microsoft', 'Apple', 'Yahoo', 'AOL', 'Excite', 'Oracle'];
    undefined
    arr.splice(2, 3, 'Google', 'Facebook');
    ["Yahoo", "AOL", "Excite"]
    arr;
    ["Microsoft", "Apple", "Google", "Facebook", "Oracle"]
    arr.splice(2,2);
    ["Google", "Facebook"]
    View Code

    5.数组的拼接

    arr;
    ["Microsoft", "Apple", "Oracle"]
    var lst = ['A','B','C'];
    undefined
    var lstadd = arr.concat(lst);
    undefined
    lstadd;
    ["Microsoft", "Apple", "Oracle", "A", "B", "C"]
    lstadd.join('-');
    "Microsoft-Apple-Oracle-A-B-C"
    View Code

    6.多维数组

    三.对象

    var xiaoming = {
        name: '小明',
        birth: 1990,
        school: 'No.1 Middle School',
        height: 1.70,
        weight: 65,
        score: null
    };
    undefined
    xiaoming.name;
    "小明"
    xiaoming['name'];
    "小明"
    'name' in xiaoming;
    true
    xiaoming.hasOwnProperty('name');
    true
    View Code

    四.循环

    1.数组

    var arr = ['Apple', 'Google', 'Microsoft'];
    for (var i = 0; i <= arr.length; i++) {
        alert(arr[i]);
    }
    View Code

    2.列表

    var arr = 'sdadda';
    for (var i = 0; i <= arr.length; i++) {
        alert(arr[i]);
    }
    View Code

    3.对象(类似python的dict)

    var o = {
        name: "jack",
        age: 20,
        city: 'Beijing'
    };
    for (var key in o){
        alert(o[key]);
    }
    View Code

    五.Map/set(Map:类似python的dict)

    1.Map取值

    var m = new Map([
        ['Michael', 95], ['Bob', 75], ['Tracy', 85]
        ]);
    alert(m.get('Michael'));
    View Code

    2.Map增/删值

    var m = new Map();
    m.set('alex',67);
    m.set('bob',59);
    alert(m.has('bob'));
    m.delete('bob');
    alert(m.get('alex'));
    View Code

    3.Set(去重,类似python列表的newlst = set(lst))

    var s2 = new Set([1, 2, 3]); 
    undefined
    s2;
    Set {1, 2, 3}
    s2.add(4);
    Set {1, 2, 3, 4}
    s2.delete(2);
    true
    s2;
    Set {1, 3, 4}
    all.js:1 loading comments...
    s2.add(4);
    Set {1, 3, 4}
    View Code

    六.迭代器(ArrayMapSet都属于iterable类型,用for..of迭代---PS:for..of是对for..in的修复)

    1.for..of

    var a = ['A','B','C'];
    var s = new Set(['A', 'B', 'C']);
    var m = new Map([[1,'x'],['name','y'],[3,'z']]);
    
    for (var x of a){
        alert(x);
    }
    for (var x of s){
        alert(x);
    }
    for (var x of m){
        alert(x[0]+'='+x[1]);
    }
    View Code

    2.forEach

    //array
    var a = ['A', 'B', 'C'];
    a.forEach(function (element, index, array) {
        // element: 指向当前元素的值
        // index: 指向当前索引
        // array: 指向Array对象本身
        alert(element);
    });
    
    //Map
    var m = new Map([[1, 'x'], [2, 'y'], [3, 'z']]);
    m.forEach(function (value,key,map){
        alert(value);
        alert(key);
    });
    
    //set无索引
    var s = new Set(['A', 'B', 'C']);
    s.forEach(function (element, set) {
        alert(element);
    });
    View Code
  • 相关阅读:
    Android——ListView学习笔记(一)
    记录笔记——关于request.getRequestDispatcher().forward(request, response)的跳转问题
    Python + openCV 实现图像垂直投影和水平投影
    2020年-第三周助教总结-第二组
    Python记录——字符串的常用方法
    2020年-第二周助教总结-第二组
    Android开发——三种活动跳转方式
    2020年-第一周助教总结-第二组
    Python —— 实例化ndarray对象
    海信聚好看矫恒浩:构建异地双活混合云,利用公共云应对流量突增
  • 原文地址:https://www.cnblogs.com/alexkn/p/4764864.html
Copyright © 2011-2022 走看看