JavaScript高级程序设计(第三版)第五章 引用类型

时间:2023-03-10 04:24:20
JavaScript高级程序设计(第三版)第五章 引用类型

5.2 Array类型

 var colors = new Array(3);      //创建一个包含3项的数组
var names = new Array("Greg"); //创建一个包含1项,即字符串“Greg”的数组

5.2.2 转换方法

 var colors = ["red", "blue", "green"];    //创建一个包含3个字符串的数组
alert(colors.toString()); //red,blue,green
alert(colors.valueOf()); //red,blue,green
alert(colors); //red,blue,green
 var colors = ["red", "green", "blue"];
alert(colors.join(",")); //red,green,blue
alert(colors.join("||")); //red||green||blue

5.2.3 栈方法

 var colors = ["red", "blue"];
colors.push("brown"); //添加另一项
colors[3] = "black"; //添加一项
alert(colors.length); //4 var item = colors.pop(); //取得最后一项
alert(item); //"black"

5.2.4 队列方法

 var colors = new Array();                      //创建一个数组
var count = colors.push("red", "green"); //推入两项
alert(count); //2 count = colors.push("black"); //推入另一项
alert(count); //3 var item = colors.shift(); //取得第一项
alert(colors.length); //2

5.2.5 重排序方法

 var values = [1, 2, 3, 4, 5];
values.reverse();
alert(values); //5,4,3,2,1
 function compare(value1, value2) {
if (value1 < value2) {
return 1;
} else if (value1 > value2) {
return -1;
} else {
return 0;
}
} var values = [0, 1, 5, 10, 15];
values.sort(compare);
alert(values); //15,10,5,1,0

5.2.6 操作方法

 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
 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,返回的数组中只包含 一项

5.2.7 位置方法

 var numbers = [1,2,3,4,5,4,3,2,1];

 alert(numbers.indexOf(4));        //3
alert(numbers.lastIndexOf(4)); //5 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

5.2.8 迭代方法

  • every():对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true。
  • filter():对数组中的每一项运行给定函数,返回该函数会返回true的项组成的数组。
  • forEach():对数组中的每一项运行给定函数。这个方法没有返回值。
  • map():对数组中的每一项裕兴给定的函数,返回每次函数调用的结果组成的数组。
  • some():对数组中的每一项运行给定的函数,如果该函数对任一项返回true,则返回true。
 var numbers = [1,2,3,4,5,4,3,2,1];

 var everyResult = numbers.every(function(item, index, array){
return (item > 2);
}); alert(everyResult); //false var someResult = numbers.some(function(item, index, array){
return (item > 2);
}); alert(someResult); //true
 var numbers = [1,2,3,4,5,4,3,2,1];

 var filterResult = numbers.filter(function(item, index, array){
return (item > 2);
}); alert(filterResult); //[3,4,5,4,3]
         var numbers = [1,2,3,4,5,4,3,2,1];

         var mapResult = numbers.map(function(item, index, array){
return item * 2;
}); alert(mapResult); //[2,4,6,8,10,8,6,4,2]