Javascript数组
来源:互联网 发布:越狱重启后软件消失 编辑:程序博客网 时间:2024/06/09 22:14
Array 类型
var colors = ["red", "blue", "green"]; // 创建一个包含 3 个字符串的数组colors[colors.length] = "black"; //(在位置 3)添加一种颜色colors[colors.length] = "brown"; //(在位置 4)再添加一种颜色
1,检测数组
最后一项的索引始终是 length-1
if (value instanceof Array); if (Array.isArray(value))
2,转换方法
var colors = ["red", "blue", "green"]; // 创建一个包含 3 个字符串的数组 alert(colors.toString()); // red,blue,green alert(colors.valueOf()); // red,blue,green alert(colors); // red,blue,green
3,添加数组
var colors = new Array(); var count = colors.push("red", "green");
var colors = new Array(); var count = colors.unshift("red", "green"); //添加两项 alert(count); //2
4,取数组最后一位
var colors =["red","green","yellow"]; var item = colors.pop(); alert(item) //yellowalert(colors.length); //2
5,取数组第一位
var colors =["red","green","yellow"]; var item = colors..shift((); alert(item) //redalert(colors.length); //2
6,数组排序
反转数组
var values = [1, 2, 3, 4, 5]; values.reverse(); alert(values); //5,4,3,2,1
从小到大
var values = [0, 1, 5, 10, 15]; values.sort(compare);alert(values); //0,1,5,10,15 function compare(value1, value2){ return value2 - value1; }
7,合并数组
将接收到的参数添加到这个数组的末尾
var colors = ["red", "green", "blue"]; var colors2 = colors.concat("yellow", ["black", "brown"]); alert(colors); //red,green,blue alert(colors2); //red,green,blue,yellow,black,brown
8,删除数组元素
slice(),它能够基于当前数组中的一或多个项创建一个新数组。slice()方法可以
接受一或两个参数,即要返回项的起始和结束位置。在只有一个参数的情况下,slice()方法返回从该
参数指定位置开始到当前数组末尾的所有项。如果有两个参数,该方法返回起始和结束位置之间的项—
—但不包括结束位置的项。
var colors = ["red", "green", "blue", "yellow", "purple"]; var colors2 = colors.slice(1); var colors3 = colors.slice(1,4); alert(colors2); //green,blue,yellow,purple alert(colors3); //green,blue,yellow var colors = ["red", "green", "blue"]; var removed = colors.splice(0,1); // 删除第一项 alert(colors); // green,blue alert(removed); // red,返回的数组中只包含一项 removed = colors.splice(1, 0, "yellow", "orange"); // 从位置 1 开始插入两项 alert(colors); // green,yellow,orange,blue alert(removed); // 返回的是一个空数组 removed = colors.splice(1, 1, "red", "purple"); // 插入两项,删除一项 alert(colors); // green,red,purple,orange,blue alert(removed); // yellow,返回的数组中只包含一项
9,数组位置方法
indexOf()和 lastIndexOf()。这两个方法都接收
两个参数:要查找的项和(可选的)表示查找起点位置的索引。
indexOf()方法从数组的开头(位置 0)开始向后查找
lastIndexOf()方法则从数组的末尾开始向前查找。
var numbers = [1,2,3,4,5,4,3,2,1]; alert(numbers.indexOf(4)); //3 alert(numbers.lastIndexOf(4));alert(numbers.indexOf(4, 4)); //5 alert(numbers.lastIndexOf(4, 4)); //3 var person = { name: "Nicholas" }; var people = [{ name: "Nicholas" }]; var morePeople = [person]; alert(people.indexOf(person)); //-1 alert(morePeople.indexOf(person)); //0
阅读全文
0 0
- javascript 数组
- JavaScript 数组
- Javascript数组
- javascript数组
- JavaScript 数组
- JavaScript 数组
- javascript数组
- javascript 数组
- javascript数组
- javaScript 数组
- JavaScript数组
- javascript数组
- javascript 数组
- javascript数组
- javascript数组
- JavaScript数组
- javascript数组
- javascript 数组
- 坚持#第239天~Shell脚本
- 静态链表的申请(栈上)
- 165. Compare Version Numbers
- 外观模式
- lucene
- Javascript数组
- Mac:miss xcrun
- 偶遇曾经的代码
- 一周一次课(12月11日)1.1-1.5
- Python爬虫(四):爬取136书屋小说,并保存至本地文本文件中,单进程多进程对比效率(以三生三世十里桃花为例)
- Maven(插件配置和生命周期的绑定)
- 虚拟机中 Linux 连不上网原因
- 171210 逆向-Take the maze
- Dagger2,一步一步到理解到使用