Daily javascript Summary (Array array)
- 2020-10-31 21:36:42
- OfStack
1. Common methods of arrays
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, the array map() method
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
//item Array element index Element corresponding index array The original array
console.log(array === numbers);//true
return item * 2;
});
console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
3, array reduce() method
var values = [1,2,3,4,5];
// receive 1 I'm going to go from left to right item Until the reduce to 1 A value.
var sum = values.reduce(function(prev, cur, index, array){
console.log(array === values);
console.log(index);//1,2,3,4 Array index from 1 start
return prev + cur;// Add the two values before and after
});
alert(sum);//15
4, array concat() method
//concat() Method is used to join two or more arrays.
// This method does not change the existing array, but only returns the connected array 1 A copy.
// grammar
//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. Array length 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. Array methods every and some
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
//item Array element index Element corresponding index array The original array
console.log(array === numbers);//true
return item * 2;
});
console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
0
7, array filter() method
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
//item Array element index Element corresponding index array The original array
console.log(array === numbers);//true
return item * 2;
});
console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
1
8. Arrays indexOf and lastIndexOf
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
//item Array element index Element corresponding index array The original array
console.log(array === numbers);//true
return item * 2;
});
console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
2
9. Arrays toLocaleString and toString
var person1 = {
toLocaleString : function () {
return "Nikolaos";
},
toString : function() {
return "Nicholas";
}
};
var person2 = {
toLocaleString : function () {
return "Grigorios";
},
toString : function() {
return "Greg";
}
};
var people = [person1, person2];
alert(people); //Nicholas,Greg
alert(people.toString()); //Nicholas,Greg
alert(people.toLocaleString()); //Nikolaos,Grigorios
10. Array push and pop methods
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
//item Array element index Element corresponding index array The original array
console.log(array === numbers);//true
return item * 2;
});
console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
4
11. Array methods unshift and shift
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
//item Array element index Element corresponding index array The original array
console.log(array === numbers);//true
return item * 2;
});
console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
5
12. Reverse array reverse
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
//item Array element index Element corresponding index array The original array
console.log(array === numbers);//true
return item * 2;
});
console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
6
13, array sorting method sort
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
//item Array element index Element corresponding index array The original array
console.log(array === numbers);//true
return item * 2;
});
console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
7
14. Array method slice
/*
slice() Method returns the selected element from an existing array.
grammar
arrayObject.slice(start,end)
start A necessity. Specify where to start. If it is negative, it specifies the position from the end of the array. In other words, -1 Refers to the final 1 An element, -2 Refers to the bottom first 2 Elements, and so on.
end Optional. Specify where to end the selection. This parameter is the array index at the end of the array fragment. If this parameter is not specified, the sharded array contains the start All elements to the end of the array. If the argument is negative, it specifies the element that is counted from the end of the array.
The return value
return 1 A new array containing an array from start to end Not including the element arrayObject Element.
*/
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
15. Array method splice
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
//item Array element index Element corresponding index array The original array
console.log(array === numbers);//true
return item * 2;
});
console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
9
16, array isArray() method
alert(Array.isArray([])); //true
alert(Array.isArray({})); //false
The above is the summary of today's javascript study, and I will continue to update it every day. I hope you will continue to pay attention.