每天一篇javascript学习小结(Array数组)

1、数组常用方法

var colors = ["red", "blue", "green"]; //creates an array with three strings alert(colors.toString()); //red,blue,green alert(colors.valueOf()); //red,blue,green alert(colors); //red,blue,green

2、数组map()方法
 

var numbers = [1,2,3,4,5,4,3,2,1]; var mapResult = numbers.map(function(item, index, array){ //item 数组元素 index元素对应索引 array原数组 console.log(array === numbers);//true return item * 2; }); console.log(mapResult); //[2,4,6,8,10,8,6,4,2]

3、数组reduce()方法

var values = [1,2,3,4,5]; //接收一个函数,然后从左到右遍历item,直到reduce到一个值。 var sum = values.reduce(function(prev, cur, index, array){ console.log(array === values); console.log(index);//1,2,3,4 数组的索引从1开始 return prev + cur;//前后两个值相加 }); alert(sum);//15

4、数组concat()方法

//concat() 方法用于连接两个或多个数组。 //该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。 //语法 //arrayObject.concat(arrayX,arrayX,......,arrayX) 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

5、数组长度length

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" alert(colors.length);//3 alert(names.length);//1

var colors = ["red", "blue", "green"]; //creates an array with three strings var names = []; //creates an empty array var values = [1,2,]; //AVOID! Creates an array with 2 or 3 items var options = [,,,,,]; //AVOID! creates an array with 5 or 6 items alert(colors.length); //3 alert(names.length); //0 alert(values.length); //2 (FF, Safari, Opera) or 3 (IE) alert(options.length); //5 (FF, Safari, Opera) or 6 (IE)

var colors = ["red", "blue", "green"]; //creates an array with three strings colors.length = 2; alert(colors[2]); //undefined

var colors = ["red", "blue", "green"]; //creates an array with three strings colors.length = 4; alert(colors[3]); //undefined

var colors = ["red", "blue", "green"]; //creates an array with three strings colors[colors.length] = "black"; //add a color colors[colors.length] = "brown"; //add another color alert(colors.length); //5 alert(colors[3]); //black alert(colors[4]); //brown

var colors = ["red", "blue", "green"]; //creates an array with three strings colors[99] = "black"; //add a color (position 99) alert(colors.length); //100

6、数组方法every和some

//every()与some()方法都是JS中数组的迭代方法。 //every()是对数组中的每一项运行给定函数,如果该函数对每一项返回true,则返回true。 //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

7、数组filter()方法

//从数组中找到适合条件的元素(比如说大于某一个元素的值) 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]

8、数组indexOf和lastIndexOf

//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。 //语法 //stringObject.indexOf(searchvalue,fromindex) //searchvalue 必需。规定需检索的字符串值。 //fromindex 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。 /* lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。 语法 stringObject.lastIndexOf(searchvalue,fromindex) searchvalue 必需。规定需检索的字符串值。 fromindex 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。 */ 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

9、数组toLocaleString和toString

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgzfpx.html