javascript学习笔记--数组的方法

1.向数组添加元素
a..length属性的形式向数组末尾添加一个元素:

   var arr = new Array():
    arr[1]=2;
   arr[arr.length]=10
    //console.log(arr) ; [ ,1, 10]

b.方法形式向数组末尾添加一个或者多个元素,并且返回数组的新长度:

var arr=[1,2,3];
var arrLength=arr.push("tom","harry");
console.log(arr);//[1, 2, 3, "tom", "harry"]
console.log(arrLength); //5

2.删除数组的最后一个元素

    var arr = ["hollo","woeld","mecomeback"];
    console.log(arr); // ["hollo","woeld","mecomeback"];
    console.log(arr.length);//3
    arr.pop();
    var arrLength=arr.pop();
    console.log(arrLength);//mecomeback
    console.log(arrLength.length)//1  删除的元素长度

3.向开头添加一个元素或者多个元素

    var arr=["tom","jeck","lilly"];
    console.log(arr);  // tom", "jeck", "lilly"
    
    arr.unshift("你好","好的吧");  
    console.log(arr);   //["你好", "好的吧", "tom", "jeck", "lilly"]

    var newarr=  arr.unshift();
    console.log(newarr); //5

4.删除数组第一个元素

  var arr=["tom","jeck","lilly"];  //["tom", "jeck", "lilly"]
  console.log(arr); 
  arr.shift();
  console.log(arr);

你可能感兴趣的:(javascript学习笔记--数组的方法)