获取字符串末尾项的方法:
var a ="erjhghjadsjf";
console.log(a[a.length-1]);
或者
var a ="erjhghjadsjf";
console.log(a.charAt(a.length-1));//charAt() 方法可返回指定位置的字符。
获取数组末尾项的方法:
var arr =[1,2,3,4,5,6,7,9];
console.log(arr[arr.length -1]);
2.pop()
pop() 方法从数组中删除最后一个元素:
实例
var fruits =["Banana","Orange","Apple","Mango"];
fruits.pop();// 从 fruits 删除最后一个元素("Mango")pop() 方法返回“被弹出”的值:
实例
var fruits =["Banana","Orange","Apple","Mango"];var x = fruits.pop();// x 的值是 "Mango"
3.shift()
位移与弹出等同,但处理首个元素而不是最后一个。
shift() 方法会删除首个数组元素,并把所有其他元素“位移”到更低的索引。
var fruits =["Banana","Orange","Apple","Mango"];
fruits.shift();// 从 fruits 删除第一个元素 "Banana"shift() 方法返回被“位移出”的字符串:
var fruits =["Banana","Orange","Apple","Mango"];
fruits.shift();// 返回 "Banana"
4.unshift()
unshift() 方法(在开头)向数组添加新元素,并“反向位移”旧元素:
var fruits =["Banana","Orange","Apple","Mango"];
fruits.unshift("Lemon");// 向 fruits 添加新元素 "Lemon"
亲自试一试
unshift() 方法返回新数组的长度。
var fruits =["Banana","Orange","Apple","Mango"];
fruits.unshift("Lemon");// 返回 5
5.push()
push() 方法(在数组结尾处)向数组添加一个新的元素:
var fruits =["Banana","Orange","Apple","Mango"];
fruits.push("Kiwi");// 向 fruits 添加一个新元素push() 方法返回新数组的长度:
var fruits =["Banana","Orange","Apple","Mango"];var x = fruits.push("Kiwi");// x 的值是 5