Skip to content

Commit 00e7e6a

Browse files
[Term Entry] JavaScript Arrays: from() (#7348)
1 parent fc69025 commit 00e7e6a

File tree

1 file changed

+112
-0
lines changed
  • content/javascript/concepts/arrays/terms/from

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
Title: '.from()'
3+
Description: 'Creates a new Array instance from an iterable or array-like object.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Web Development'
7+
Tags:
8+
- 'Arrays'
9+
- 'Functions'
10+
- 'JavaScript'
11+
- 'Methods'
12+
CatalogContent:
13+
- 'introduction-to-javascript'
14+
- 'paths/front-end-engineer-career-path'
15+
---
16+
17+
The **`Array.from()`** static method creates a new `Array` instance from an iterable or array-like object. It provides a convenient way to convert various data structures into arrays, making them easier to work with using array methods.
18+
19+
## Syntax
20+
21+
```pseudo
22+
Array.from(arrayLike, mapFn, thisArg)
23+
```
24+
25+
**Parameters:**
26+
27+
- `arrayLike`: An iterable or array-like object to convert to an array
28+
- `mapFn` (optional): A function to call on every element of the array being created
29+
- `thisArg` (optional): Value to use as `this` when executing `mapFn`
30+
31+
**Return value:**
32+
33+
The `Array.from()` method returns a new Array instance.
34+
35+
## Example 1: Basic Array Creation Using `Array.from()`
36+
37+
This example demonstrates the fundamental usage of `Array.from()` to create an array from a string:
38+
39+
```js
40+
// Convert string to array of characters
41+
const text = 'hello';
42+
const charArray = Array.from(text);
43+
44+
console.log(charArray);
45+
```
46+
47+
The output of this code is:
48+
49+
```shell
50+
['h', 'e', 'l', 'l', 'o']
51+
```
52+
53+
The `Array.from()` method treats the string as an iterable object, creating a new array where each character becomes an individual element.
54+
55+
## Example 2: Using `Array.from()` to Convert Set to Array
56+
57+
This example shows how to convert a Set data structure to an array, which is useful when there is a need to use array methods on unique values:
58+
59+
```js
60+
// Create a Set with unique values
61+
const uniqueNumbers = new Set([1, 2, 2, 3, 3, 4]);
62+
63+
// Convert Set to array
64+
const numberArray = Array.from(uniqueNumbers);
65+
66+
console.log(numberArray);
67+
68+
// Use array methods
69+
const doubled = numberArray.map((num) => num * 2);
70+
console.log(doubled);
71+
```
72+
73+
The output of this code is:
74+
75+
```shell
76+
[1, 2, 3, 4]
77+
[2, 4, 6, 8]
78+
```
79+
80+
This approach is particularly valuable when working with data that needs to maintain uniqueness during processing but requires array functionality for further manipulation.
81+
82+
## Codebyte Example: Array Creation with Mapping
83+
84+
This example demonstrates using the optional mapping function parameter to transform elements during array creation:
85+
86+
```codebyte/javascript
87+
// Create an array of numbers and square them simultaneously
88+
const numbers = [1, 2, 3, 4, 5];
89+
const squaredNumbers = Array.from(numbers, x => x * x);
90+
91+
console.log(squaredNumbers);
92+
93+
// Generate a sequence of numbers from 0 to 4
94+
const sequence = Array.from({length: 5}, (_, index) => index);
95+
console.log(sequence);
96+
```
97+
98+
The mapping function provides an efficient way to create and transform arrays in a single operation, eliminating the need for separate `map()` calls.
99+
100+
## Frequently Asked Questions
101+
102+
### 1. How to get array from set in JS?
103+
104+
Use `Array.from(yourSet)` to convert a Set to an array. For example: `const array = Array.from(new Set([1, 2, 3]));` This creates `[1, 2, 3]`.
105+
106+
### 2. What is the difference between `Array.from()` and the spread operator?
107+
108+
While both can convert iterables to arrays, `Array.from()` offers additional functionality with its optional mapping function parameter, allowing transformation during conversion. The spread operator `[...iterable]` only converts without transformation.
109+
110+
### 3. Can `Array.from()` work with array-like objects?
111+
112+
Yes, `Array.from()` works with both iterable objects (like Sets, Maps, strings) and array-like objects (like NodeList, arguments object). It creates a shallow copy and converts them to proper arrays with all array methods available.

0 commit comments

Comments
 (0)