JavaScript-Array type (local object)

Change the original array: reverse, sort, push, pop, shift, unshift, splice
does not change the original array: concat, join, splite, slice

Each item in the ECMAScript array can hold any type of data.
The size of the array can be adjusted dynamically, that is, it can automatically grow with the addition of data to accommodate new data.

There are two basic ways to create an array:

  1. Array constructor var color = new Array (); var colors = new Array (20); length = 20
    If you know the number of saved items in advance, you can also pass that number to the constructor. You can also pass the items that should be contained in the array to the Array constructor.
    var colors = new Array ("blue", "green", "red"); The
    new operator can be omitted when using the Array constructor
  2. Using array literal notation Array literals are represented
    by a pair of square brackets containing array items, and multiple array items are separated by commas.
    var colors = ["red", "blue", "block"];
    var name = []; create an empty array

The length property of the array is very characteristic, it is not read-only. You can remove items from the end of the array or add new items to the array. All new items are undefined.

The problem with the instanceof operator is that it assumes that there is only one global execution environment. If the webpage contains multiple frames, there are actually more than two different global execution environments, and thus there are more than two different versions of the Array constructor. To solve this problem, ECMAScript5 added Array.isArray () method. The purpose of this method is to finally determine whether a value is an array, regardless of the global execution environment in which it was created. if (Array.isArray (value)) {…}

Conversion method

All objects have toLocationString (), toString (), and valueof () methods.
Calling the toString () method of the array will return a comma-separated string of each string in the array.
Calling valueof () returns an array
alert () to receive string parameters, so when toString () method is
called in the background , toArrayString () method of the array is also created, and an array of comma-separated strings is also created. Get the toLocationString () method of the array, not the toString () method.

Stack method

The stack is a last-in, first-out data structure. The insertion (pushing) and removal (popping) of items in the stack
return the length of the modified array from the push () and pop () methods that occur at the top of the stack . Inserting the last
pop () added to the array returns the removed item. Delete the last element of the array

Queue method

Queue method, first-in-first-out, the queue adds items at the end of the list and removes items from the front of the list. Get the item
shift () from the front of the array to remove the first item in the array and return the item. Remove
unshift () to add any number of items at the front of the array and return the length of the new array. insert

Reordering method

reverse () and sort ()
sort () sorts from small to large, will call the toString () method of each array item, compare the resulting string, and have determined how to sort
var values ​​= [0,1,5,10,15 ];
values.sort ();
alert (values); // 0,1,10,15,5
calling the sort () method will change the original order according to the test string results, not the best solution
sort () method Can receive a comparison function as a parameter, so that we can specify which value is in front of which value

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

For numeric types or its valueof () method will return numeric type object type

function compare(value1,value2){
	return value2 - value1;
}

Method of operation

  1. The concat () method first creates a copy of the current array, adds the received parameters to the end of the copy, and finally returns the newly constructed array. (Include, increase)
  2. The slice () method receives one or two parameters, that is, the start and end of the item to be returned. (Interception) Does not affect the original array
    One parameter, returns all items from the specified position to the end of the current array
    Two parameters, returns the items between the start and end, but does not include the items at the end position
  3. The splice () method inserts items into the middle of the array.
    Delete: 2 parameters, the position of the first item to be deleted and the number of items to be deleted. splice (0,2) deletes the first two
    items. Insert: 3 parameters, starting position, 0 (number of items to be deleted), item to be inserted. splice (2,0, "green", "red"); Insert the string "green", "red" from the position 2 of the current array
    Replace: any number of items inserted at the specified position, and delete any number of items at the same time , 3 parameters: the starting position, the number of items to be deleted and any number of items to be inserted. The number of inserted items need not be equal to the deleted items. splice (2,1, "red", "green"); delete the item at position 2 of the current array and insert from position 2. splice () returns the items deleted from the original array (no items deleted, returns an empty array)

Location method

indexOf () The item to search backward from the beginning and (optionally) the index indicating the starting point of the search.
lastIndexOf () The item searched from the end to the front and (optionally) the index of the starting point of the search.
var numbers = [1,2,3,4,5,4,3,2,1];
alert (number.indexOf (4)); // 3
alert (number.lastIndexOf (4)); // 5

Iterative method

Each method will have a function that runs. This function receives three parameters: the value of the array item, the position of the item in the array, and the array object itself.

  1. every () runs the given function on each item in the array and returns true if the function returns true for each item

  2. filter () runs the given function on each item in the array and returns an array of items that the function returns true

  3. foreach () runs the given function on each item in the array, no return value

  4. map () runs the given function on each item in the array and returns an array of the results of each function call

  5. some () runs the given function on each item in the array, and returns true if the function returns true for any item

     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
    
  6. map () returns an array, and each item of this array is the result of running the passed function on the corresponding item in the original array. Example: Multiply each item of the array by 2 and return the array of products

     var numbers = [1,2,3,4,5,4,3,2,1];
     var everyResult = numbers.map(function(item,index,array){
     		return (item * 2);
     	})
     alert(everyResult);//[2,4,6,8,10,8,6,4]
     1,2,3,4,5都不会修改数组中包含值
    

Merge method

The two methods reduce () and reduceRight () iterate over all the items in the array, and then construct a final return value. reduce () starts from the first item of the array and traverses one by one to the end, and reduceRight () is the opposite. Both methods receive two parameters: a function called on each item and (optionally) the initial value that is the basis of the merge.
4 parameters: previous value, current value, item index and array object. Any value returned by this function will be automatically passed to the next item as the first parameter.
Use the reduce () method to perform the operation of summing all the values ​​in the array.

var values = [1,2,3,4,5];
var sum = values.reduce(function(pre,cur,index,array){
			return pre + cur;
})
alert(sum);//15
第一次执行回调函数,prev是1,cur是2,第二次,prev是3,(1+2=3),cur是3(数组的第三项),这个过程会持续到把数组中的每一项访问一遍,返回结果。
Published 17 original articles · liked 0 · visits 770

Guess you like

Origin blog.csdn.net/CandiceYu/article/details/89888723