First, the concept of arrays
[]
a collection of data of a set of ordered, arbitrary data types, wrapped together
The length of the array:array.length
There is a big difference between an array in JS and an array of other languages.
Second, the way to create an array of 1. Through constructors
2. by literal
var arr = [];
Third, detection is not an array (3 methods) 1, borrowed object prototype
toString() 方法
---> "The most rigorous, good compatibility"
<script> var arr = []; var result = toString.call(arr); console.log(result);</script>若是数组,输出: ‘[object Array]‘
2.
Array.isArray(被检测对象)
---> ES5 new methods, compatibility issues
<script> var arr = []; var result = Array.isArray(arr); console.log(result);</script>// 结果:布尔值
3. Use
instanceof
Determine what type of reference---> IE8 and the following are incompatible
Usage:被检测对象 instanceof 引用类型的构造函数
Reference type values are: Object, Array, RegExp, Data, and so on
任何对象 instanceof Object
Value is true (because all objects inherit from: Object)
<script> var arr = []; var result1 = arr instanceof Array; // true var result2 = arr instanceof Object; // true</script>
4. Detecting array Compatible Code: combination of methods 1 and 2
综上:测试数组兼容代码if(typeof Array.isArray==="undefined"){ Array.isArray = function(arg){ return result = Object.prototype.toString.call(arg)==="[object Array] ? true : false" }; }
Iv. Method of conversion
- All objects have toString (), tolocalestring (), ValueOf () methods;
- Call the array's toString (): string concatenation of each value in the array, separated by a comma
- Call an array of tolocalestring ():
- ValueOf () of the calling object:
Five, array method 1. Iterative methods----> Traversal
ForEach ()Traversing the entire array
Return is not allowed to stop
Traverse the entire array, cannot stop, return cannot stop it, affect performance
<script> var arr = [1,2,3,4]; arr.forEach(function( value , index){ });</script>
filter ()Iterate through all, filter the entire array, match the conditions of the new array and return
Creates a new array (which will match the criteria) and returns
The original array does not change
The code for the filter criteria, written in return
<script> var arr = [1,2,3,4,23,4,3,5,6]; var result = arr.filter(function (value,index,array) { return (value < 10) }); console.log(result); // [1,2,3,4,4,3,5,6]</script>
map ()Traverse all, collectively, compose a new array and return
Creates a new array (after the collective operation) and returns
The original array does not change
Collective operation of the Code, written in the return
<script> var arr = [1,2,3,4]; var newArr = arr.map(function(value,index){ return value*10; }) console.log(newArr); console.log(arr);</script>
every ()Traverse to determine whether a condition is met, return a Boolean value
The judging condition is written in return
to determine if there is a non-conformance in the array, stop the traversal and returnfalse
Judging that all is in line, returntrue
The original array does not change
<script> var arr = [1,2,3,4]; var result = arr.every(function (value, index) { return value < 3; }); console.log(result); </script>
some ()Traverse to determine whether a condition is met, return a Boolean value
The judging condition is written in return
If there is an item in the array that matches, stop the traversal and returntrue
Judging all the non-conforming, returningfalse
The original array does not change
<script> var arr = [1,2,3,4]; var result = arr.some(function (value, index) { return value > 3; }); console.log(result); </script>
2. Stack method (
Last in, first out)
push ()Append array item at the end, cannot append array, change the original array, return the new array length
<script> var arr = [1,2,3,4]; var result = arr.push(5,6,7); console.log(arr); // [1,2,3,4,5,6,7] console.log(result); // 7 </script>
PopDelete array entries at the end, change the original array, and return the deleted array items
<script> var arr = [1,2,3,4]; var result = arr.pop(); console.log(arr); // [1,2,3] console.log(result); // 4</script>
3. Queue method
Advanced First Out
unshift ()Append array items at the beginning, cannot append array, change the original array, return the new array length
<script> var arr = [1,2,3,4]; var result = arr.push(5,6,7); console.log(arr); // [5,6,7,1,2,3,4] console.log(result); // 7 </script>
shift ()The first item of the array is deleted, the original array is changed, and the deleted array item is returned.
<script> var arr = [1,2,3,4]; var result = arr.shift(); console.log(arr); // [2,3,4] console.log(result); // 1</script>
4. How to Operate
concat ()Add array items/arrays at the end, make up a new array and return
<script> var arr = [1,2,3,4]; var arr2 = arr.concat(5,6,[7,8]); console.log(arr); // [1,2,3,4] console.log(arr2); // [1,2,3,4,5,6,7,8] </script>
Slice ()The original array is not changed, the array item is truncated, the new array is formed and returned
Parameter (start, end): Intercepts the first and the end index of an array item , not including the end
Parameter (START): Intercepts the first index of an array item , ending at the end
Parameter (): Copies a new array that can be used to: convert a pseudo-array to a true array
* 实现:var realArr = [].slice.apply(伪数组);* 原理var arr1 = arr.slice();
- Methods for copying an array
<script> var a1 = [1, 2]; var a2 = a1.slice(); console.log(a2); // [1, 2] console.log(a1 === a2); // false</script>
Splice ()The original array changes (delete, replace, add), a new array of deleted array items and returns
Parameter (index, number): the index (inclusive) of the array item to delete,
Parameter (index, number, item): To delete the index of an array item, to delete an array item, to insert an array item (multiple)
Parameter index, number is required, parameter item is optional
Added item array entry starting from index index (directly displacing position of index index)
//Delete<Script> varArr=[0, 1, 2, 3, 4, 5, 6]; varResult= arr.Splice(1, 2); Console.Log(arr); //[0, 3, 4, 5, 6] Console.Log(Result); //[1, 2]</script>
//Replace<Script> varArr=[0, 1, 2, 3, 4, 5, 6]; varResult= arr.Splice(1, 3, 7, 8); Console.Log(arr); //[0, 7, 8, 4, 5, 6] Console.Log(Result); //[1, 2, 3]</script>
//Insert<Script> varArr=[0, 1, 2, 3, 4, 5, 6]; varResult= arr.Splice(1, 0, 7, 8); Console.Log(arr); //[0, 7, 8, 1, 2, 3, 4, 5, 6] Console.Log(Result); // []</script>
5. Array to String
- The method corresponding to the string to the array corresponds to Str.split (' delimiter ')
join (' delimiter ')Array to delimiter concatenated string
- The original array does not change
Parameter (' delimiter '): Turns into a string that is split to link each array item
Parameter ('): Turns into a string (each array item has no Fuhai link)
Parameter (): A string that turns into a comma-linked array item
<script> var arr = [1, 2, 6]; console.log(arr); // [1,2,6] console.log(arr.join(‘--‘)); // ‘1--2--6‘ console.log(arr.join(‘‘)); // ‘126‘ console.log(arr.join()); // 1,2,6 这个多个字符串</script>
6. Find a location
indexOf ()Start from scratch: finds array items, returns the first index that matches the condition array item
The original array does not change, just find the location
Can only be detected: from the find starting point the first Qualifying array item index
Returns the index of an array item that matches the criteria
Exact lookup, no return-1
Parameter (value, StartIndex): value is the lookup item, StartIndex is the location to start looking for (with array items at this location)
Parameter (value): Default from the start of index 0 to find
<script> var arr = [0, 1, 2,3,4,5,6]; var result = arr.indexOf(3,3); console.log(result); // 3</script>
lastIndexOf ()Starting at the end: finds the array item and returns the first index that matches the condition array item
- Exact lookup, no return-1
7. Array Item Ordering
reverse ()Array items are reversed, and the original array is changed to an inverted array
<Script> varArr=[0, 1, 2, 3]; varResult= arr.Reverse(); Console.Log(arr); //[3, 2, 1, 0] Console.Log(Result); //[3, 2, 1, 0] Console.Log(arr===Result; //True</script>
sort ()Array item ordering (number ratio size, string size), original array changed to sorted array
Parameter (): Sort by character encoding of the string without the parameter being passed
Arguments (function expressions) "Use a minus sign between parameters, and no greater than equals"
- function, return parameter a-parameter B; ----> Ascending
- function, return parameter B-parameter A; ----> Descending
<script> var arr = [0, 1, 10, 12, 7, 22, 2, 3]; var result = arr.sort(function (a, b) { return a - b; }); console.log(arr); // [0, 1, 2, 3, 7, 10, 12, 22] console.log(result); // [0, 1, 2, 3, 7, 10, 12, 22] console.log(arr === result); // true</script>
- To sort an item in an object
<script> var obj = [{ value: 10 }, { value: 30 }, { value: 20 }]; var result = obj.sort(function(a, b) { return b.value - a.value; }); // 降序排列 console.log(result);</script>
JS Reference type---Array