JS基础知识(数组)
2013-10-30 13:01
369 查看
1,数组
shift:删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined
unshift:将参数添加到原数组开头,并返回数组的长度
pop:删除原数组最后一项,并返回删除元素的值;如果数组为空则返回undefined
push:将参数添加到原数组末尾,并返回数组的长度
concat:返回一个新数组,是将参数添加到原数组中构成的
放值:
在最后面加值:
注意:
数组最多能放4,294,967,295个元素
last-in-fi rst-out (LIFO)结构:
var colors = new Array(); var colors = new Array(20); var colors = new Array(“red”, “blue”, “green”); var colors = new Array(3); //create an array with three items var names = new Array(“Greg”); //create an array with one item, the string “Greg”
shift:删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined
var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b:1
unshift:将参数添加到原数组开头,并返回数组的长度
var a = [1,2,3,4,5]; var b = a.unshift(-2,-1); //a:[-2,-1,1,2,3,4,5] b:7
pop:删除原数组最后一项,并返回删除元素的值;如果数组为空则返回undefined
var a = [1,2,3,4,5]; var b = a.pop(); //a:[1,2,3,4] b:5//不用返回的话直接调用就可以了
push:将参数添加到原数组末尾,并返回数组的长度
var a = [1,2,3,4,5]; var b = a.push(6,7); //a:[1,2,3,4,5,6,7] b:7
concat:返回一个新数组,是将参数添加到原数组中构成的
var a = [1,2,3,4,5]; var b = a.concat(6,7); //a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]
放值:
var colors = [“red”, “blue”, “green”]; //define an array of strings alert(colors[0]); //display the first item colors[2] = “black”; //change the third item colors[3] = “brown”; //add a fourth item
在最后面加值:
var colors = [“red”, “blue”, “green”]; //creates an array with three strings colors[colors.length] = “black”; //add a color (position 3) colors[colors.length] = “brown”; //add another color (position 4)
注意:
数组最多能放4,294,967,295个元素
last-in-fi rst-out (LIFO)结构:
var colors = new Array(); //create an array var count = colors.push(“red”, “green”); //push two items alert(count); //2 count = colors.push(“black”); //push another item on alert(count); //3 var item = colors.pop(); //get the last item alert(item); //”black” alert(colors.length); //2
相关文章推荐
- JS数组基础知识总结
- js 基础知识 对象与数组
- 笔记14--js基础知识-数组
- 【Js】数组基础知识-思维导图
- JS基础知识(上)(变量,条件判断语句,循环语句,函数,对象,数组)
- javascript基础知识(数组)
- java基础知识回顾之---java String final类普通方法的应用之字符串数组排序
- 位运算的基础知识点(关联指针、数组的操作)
- (二)数组基础知识
- Node.js基础知识杂烩
- Linux-C基础知识学习:C语言作业-从键盘输入一行字符,统计其中有多少个字母, 数字,空格(用数组实现,gets)。
- java 基础知识五 数组
- js基础---函数传参 数组基础
- 基础知识(js)-----javascript的等性运算符
- 7个JS基础知识总结
- js基础知识总结
- js基础知识总结
- js基础知识整理
- JavaScript入门教程(2) JS基础知识
- 黑马程序员----------java基础知识(4)之数组