Logo

dev-resources.site

for different kinds of informations.

Which should you use? Array.from vs. Spread Operator

Published at
11/15/2023
Categories
javascript
vanilla
tutorial
discuss
Author
gingerchew
Author
10 person written this
gingerchew
open
Which should you use? Array.from vs. Spread Operator

I had a conversation with a coworker about the merits of using spread ... vs Array.from. While they look the same on the outside, they work differently.

Iterable vs. Length

If you want to spread something into an array, it needs to have a Symbol.iterator property.

This means that the following Javascript will fail:

const spreadObject = [...{ user: 'jane' }];
Enter fullscreen mode Exit fullscreen mode

Even though this would work:

const user1 = {
    user: 'eloise'
};

const updatedUserInfo = {
    lastLogin: 'today'
};

const user1New = {
    ...user1,
    ...updatedUserInfo
};
Enter fullscreen mode Exit fullscreen mode

The solution would be to add a [Symbol.iterator] generator function to the object, which is honestly not worth it.

Array.from creates an array if the object has either an iterator or .length property.

Built in mapping function

Have you ever seen a snippet like this:

const mappedArray = [...arr].map(item => {
    // do stuff to item
})
Enter fullscreen mode Exit fullscreen mode

Did you know that that creates 2 arrays? Array.from has a built in mapping function though.

const mappedArray = Array.from(arrLike, item => {
    // do stuff to item
})
Enter fullscreen mode Exit fullscreen mode

I find this helps keep things more explicit, especially when you are not using an inline function.

const approveUser = (user) => ({
    ...user,
    approved: user.age > 21
});

const allApprovedSpreadUsers = [...users].map(approveUser).every(user => user.approved);

const allApprovedMappedUsers = Array.from(users, approveUser).every(user => user.approved);
Enter fullscreen mode Exit fullscreen mode

Do you need to care about making that extra array? Probably not, but it is a neat thing to remember.

Which do you use?

I usually reach for Array.from. The spread operator is very useful when getting n arguments from a funciton, and spreading objects into another is priceless.

const mergeUserInfo {
    ...userFromSource1,
    ...userFromSource2
};

function approveMergedUsers(user1, ...otherUsers) {
    // do user stuff
}
Enter fullscreen mode Exit fullscreen mode

Either way, I think Array.from is worth keeping in your tool belt.

vanilla Article's
30 articles in total
Favicon
🎨🛠️ 𝗩𝗮𝗻𝗶𝗹𝗹𝗮 𝗙𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸 𝘁𝗼 𝗘𝗺𝗽𝗼𝘄𝗲𝗿 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿𝘀 🚀🌐
Favicon
Mastering Vanilla JavaScript and Libraries: The Road to Dynamic DOM Rendering
Favicon
🌟 Vanilla Update: New Components and Enhanced Features! 🌟
Favicon
🌐 Unlock Development with Vanilla: The Non-Framework Powerhouse 🌐
Favicon
🚀 Vanilla & CSSer Major Update! 🚀
Favicon
How to upload files to a server in NodeJS with Formidable
Favicon
🚀 Vanilla Update: A New Development Methodology! 🚀
Favicon
Secure Text Encryption and Decryption with Vanilla JavaScript
Favicon
🚀 Vanilla Framework Update: Meet CSSer! 🚀
Favicon
🌟 Vanilla & CSSer Accessibility Update! 🌟
Favicon
Storing and retrieving JavaScript objects in localStorage
Favicon
New lightbox package here!
Favicon
Data-driven UIs
Favicon
A11y: Vanilla javascript aria-live announcer
Favicon
Keyboard input in Node.js
Favicon
Javascript Made Simple!
Favicon
Array methods and when to use them, forEach, map, reduce
Favicon
Which should you use? Array.from vs. Spread Operator
Favicon
Tiny Vanilla.js Projects
Favicon
Maneras de clonar un objecto en javascript
Favicon
Vanilla JavaScript data fetch
Favicon
Vanilla(ts) configuration with Webpack. a little bit different.
Favicon
My first Chrome Extension
Favicon
Speedy Typer Game
Favicon
What is CSS @scope and why should I care?
Favicon
Path aliases with Vanilla Node JS
Favicon
Montar SPA de cero con Vanilla y Jest
Favicon
Web Apps from scratch: Forms
Favicon
Sending asynchronous POST requests with pure JS: ditching jQuery's ajax
Favicon
State Management with Vanilla JavaScript

Featured ones: