“This is the 22nd day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
Array. The from method
The array. from method is used to convert two types of objects into real arrays:
-
Array-like object (array-like object)
let arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 }; let arr = Array.from(arrayLike); // ['a', 'b', 'c'] Copy the code
The so-called array-like object has only one essential feature, that is, it must have the length attribute. Therefore, any object with a length attribute can be converted to an Array using the array. from method.
-
Iterable objects (including sets and maps)
Array.from('hello') // ['h', 'e', 'l', 'l', 'o'] let namesSet = new Set(['a', 'b']) Array.from(namesSet) // ['a', 'b'] // Both strings and Set structures have Iterator interfaces, so array. from can be converted to true arrays.Copy the code
An Array of method
The array.of () method converts a set of values to an Array.
Array.of(1, 2, 3)
Copy the code
Print result:
Flat method
Used to “flatten” a nested array into a one-dimensional array. Returns a new array, with no effect on the original data.
[1, 2, [3, 4]].flat()
Copy the code
Flat () only “flattens” one layer by default.
If you want to “flatten” a multi-tier nested array, you can write the argument to the flat() method as an integer.
[1, 2, [3, [4, 5]]].flat(2)
Copy the code
If you’re not sure how many layers there are, but you want to convert them to a one-dimensional array, you can pass the keyword Infinity
[1, 2, [3, [4, [5,6]]]]. Flat (Infinity)Copy the code
The fill method
Given a value, populate the array.
Arr. fill(value, start, end) -value: indicates the fill value. - start: indicates the start position, which can be omitted. - end: indicates the end position, which can be omitted. The actual end position is end-1.Copy the code
Suppose you already have an array and want to replace all of its values with the specified values
You can also replace data at a specified location (or within a specified location range)
CopyWithin method
Inside the current array, copies the member at the specified location to another location (overwriting the original member), and returns the current array. Modifies the current array.
Arr. CopyWithin (target, start, end) - target (required) : Replace data from this location. - start (Optional) : indicates the start location of the replication source. The default value is 0. If it is negative, it is the reciprocal. - end (Optional) : indicates the end position of the replication source. The default value is the array length. If it is negative, it is the reciprocal. The actual end position is end-1.Copy the code
Take a look at some examples:
So here we’re going to copy the elements at subscript 3-5 and overwrite them from subscript 1.
Both start and end are optional. If left blank, the entire array is copied, and if there are not enough target positions, as many as can be overwritten.
🎨 [thumbs up] [concerns] don’t get lost, more front-end dry goods waiting for you to unlock
Phase to recommend
👉 Take a look at JS prototype inheritance
👉 15 JavaScript array practical tips
👉 JavaScript array methods look at this article is enough
👉 JS guide to using the built-in Date object Date