This is the 25th day of my participation in Gwen Challenge
The array API is used frequently by JS developers and is very important in JS learning.
Arrays are basic one-dimensional data structures that are important in all kinds of programming languages. JS natural flexibility, and further play to the strengths of the array, enrich the use of the array scenario.
Leetcode also has a high proportion of array-related topics, so it is essential to be familiar with the various array apis.
Constructor for Array
The Array constructor is used to create a new Array. It is usually recommended to create an Array using object literals, such as var a = []. For example, if I want to create an empty array of length 6, I can’t create it using object literals.
// Use the Array constructor to customize the length.
var a = Array(2)/ / / the empty x 2
// Use object literals
var b = [];
b.length = 6; / / / undefined x 6
Copy the code
new Array(arg1,arg2,…) If the argument length is 0 or greater than or equal to 2, the arguments passed will be the 0th through NTH entries of the new array in any order. (If the argument length is 0, an empty array is returned.)
New Array(len) Returns an Array of len elements if len is not a number, or an Array of len elements if len is a number ([empty × len]).
It is important to note that the new Array method cannot be used to create an Array of single elements. For example, the Array [1] cannot be created from the new Array anyway, because the new Array(1) returns only [empty × 1].
New constructors in ES6: array. of and array. from
Array.of is used less overall, and array. from has flexibility.
Array.of
Array.of converts the parameters to one item in an Array and returns the new Array, regardless of whether the parameter is a number or any other data type. The Array() method doesn’t work for arrays of single numeric elements, but array.of works perfectly
A = new Array(1)
console.log(A)//[empty]
B = Array.of(1)
console.log(B)//[1]
Copy the code
For other operations, array. of is exactly the same as new Array.
Array.from
Array.from is designed to create new arrays based on other objects, specifically, to create a new Array instance from an array-like iterable. Array.from turns an object into an Array whenever it has an iterator. (Returns a new array without changing the original object)
Syntactically, array. from takes three arguments
- Array-like objects, iterable objects
- Processing function, the newly generated array is processed by this function and then returned
- This scope, the value of this when the table name processing function executes
Only the first parameter is mandatory, and the rest are optional.
var obj = {0: 'a', 1: 'b', 2:'c', length: 3}; Array.from(obj, function(value, index){ console.log(value, index, this, arguments.length); return value.repeat(3); // return value must be specified, otherwise return undefined, obj);Copy the code
You can see that a,b, and c in [“aaa”,” BBB “,” CCC “] are repeated three times. The array. from method can be used to customize the processing of the processing function, and return the desired value, if the return value is not determined, return undefined, and eventually generate an empty Array containing undefined elements.
If this is not specified, the processing function can be an arrow function at all. The above code can be abbreviated as follows
Array.from(obj,(value) = >value.repeat(3))
//["aaa","bbb","ccc"]
Copy the code
In addition to the obj object above, iterable objects include String,Set,Map, etc., array. from all can be handled.
//String
Array.from('abc') //["a","b","c"]
//Set
Array.from(new Set(['abc'.'def'])) //["abc","def"]
//Map
Array.from(new Map([1."a"], [2.'b']))//[[1,'a'],[2,'b']]
Copy the code