zoukankan      html  css  js  c++  java
  • 数组和对象常用方法汇总

    数组的常用方法

    1,shift()方法:把数组的第一个元素删除,并返回第一个元素的值

    var a = ['a', 'b', 'c'];
    console.log(a,a.shift());
    //['b','c']     'a'

    2,unshift() :将参数添加到原数组开头,并返回数组的长度 

    var movePos =[111,222,333,444];
    movePos.unshift("55555")
    document.write(movePos + "<br />")     //55555,111,222,333,444

    3,pop():用于删除并返回数组的最后一个(删除元素)元素,如果数组为空则返回undefined ,把数组长度减 1

    var a = ['a', 'b', 'c'];
    console.log(a,a.pop());
    //["a", "b"]      "c"

    4,push():可向数组的末尾添加一个或多个元素,并返回新的长度,(用来改变数组长度)。

    var movePos=[11,22];
    
    var arr=movePos.push("333");
    
    console.log(movePos,arr) //[11, 22, "333"] 3

    5,concat()方法:用于连接两个或多个数组,并返回一个新数组,新数组是将参数添加到原数组中构成的 

    var movePos=[11,22];
    var arr=movePos.concat(4,5);
    console.log(arr);//[11, 22, 4, 5]

    6,join()方法:用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的。

    var movePos=[11,22];
    var arr=movePos.join("+");
    console.log(arr)  //11+22

    7,slice()方法:可从已有的数组中返回选定的元素。slice(开始截取位置,结束截取位置)

    var movePos=[11,22,33];
    var arr=movePos.slice(1,2);
    console.log(arr)//[22]

    8,splice()方法:方法向/从数组中添加/删除项目,然后返回被删除的项目。

    var movePos=[11,22,33,44];
    var arr=movePos.splice(1,2);//删除
    console.log(arr,movePos)
     [22, 33]     [11, 44]

      var movePos =[111,222,333,444];
      movePos.splice(2,1,"666")
      console.log(movePos)

      [111, 222, "666", 444]

    --------------------

    split:方法用于把一个字符串分割成字符串数组。

    var host="?name=232&key=23";
    host=host.split("?")
    console.log(host)
    ["", "name=232&key=23"]

    substring() 方法用于提取字符串中介于两个指定下标之间的字符。

    var host="?name=232&key=23";
    host=host.substring(1)
    console.log(host)
    //name=232&key=23

    剩下的有时间在不上

    参考:https://www.cnblogs.com/js0618/p/6283724.html

    日常所遇,随手而记。
  • 相关阅读:
    SQL Server(00):约束Constraint
    SQL Server(00):T-SQL批处理
    SQL Server(00):事务
    SQL Server(00):锁
    SQL Server(00):表变量和临时表
    SQL Server(00):T-SQL游标
    SQL Server(00):用户自定义函数(UDF)
    SQL Server(00):存储过程Stored Procedure
    C#(99):微软报表A4纸大小规则
    C#(99):C#互操作
  • 原文地址:https://www.cnblogs.com/zhihou/p/8960993.html
Copyright © 2011-2022 走看看