How to conditionally spread objects in Javascript

Introduction

In Javascript, If we want to spread an object into another one, we usually use the spread syntax operator (...) to do so. Like the following:

const user = {
    fname: 'John',
    lName: 'Doe'
};

const userDetails = {
    ...user
};

console.log(userDetails);
// Output
// { fname: 'John', lName: 'Doe' }

In this article, we gonna see how to conditionally spread object properties into another one using the spread operator. In other words, we need to populate the object properties only if a certain condition happened.


Conditionally spread objects

To do so, we can short-circuit the spread operator using the && operator.

Meaning, that if the condition is met, the spread operator will be executed and the properties of the object will be spread into the other one. Otherwise, it will be skipped.

Example

const isPremiumUser = true;

const user = {
    fname: 'John',
    lName: 'Doe'
};

const premiumUser = {
    ...(isPremiumUser && user)
};

console.log(premiumUser);
// Output: { fname: 'John', lName: 'Doe' }

In the example above, we’ve declared the user object with some properties and a boolean flag isPremiumUser = true. In line8, we’ve declared the premiumUser as an object, and the value of this object would be filled with the properties from the user object only if the following condition is true.

isPremiumUser && user

Would be evaluated to be true if:

  • isPremiumUser = true
  • The user object is of type object (not null/undefined)

That’s it for how to conditionally spread objects in Javascript and as always happy coding!

Photo from Unsplash.


Related posts

Related Posts

How to Capture Screenshots with Puppeteer In NodeJS

How to Capture Screenshots with Puppeteer In NodeJS

To Capture Screenshots with Puppeteer: Launch a Browser Instance Navigate to the Web Page Capture the Screenshot Introduction: Puppeteer is a powerful Node.js library that allows developers…

How to Minimize Puppeteer Browser Window To Tray

How to Minimize Puppeteer Browser Window To Tray

Puppeteer is a powerful tool for automating tasks in headless or non-headless web browsers using JavaScript. While Puppeteer is often used to perform actions within a browser,…

Intercepting Responses in Node.js with Puppeteer

Intercepting Responses in Node.js with Puppeteer

Introduction: Puppeteer is a powerful Node.js library that provides a high-level API for controlling headless Chrome or Chromium browsers. It’s widely used for web scraping, automated testing,…

Mastering React Component Re-rendering in Jest

Mastering React Component Re-rendering in Jest

In this hands-on guide, we’ll explore the art of optimizing React component re-rendering within Jest tests. By combining theory with practical coding examples, you’ll gain a deep…

Eliminating Nesting Loops in React Rendering

Eliminating Nesting Loops in React Rendering

React has ushered in a new era of web application development with its component-based structure, promoting code reusability and maintainability. But as projects evolve, achieving optimal performance…

Exploring Type and Interface Usage in TypeScript

Exploring Type and Interface Usage in TypeScript

TypeScript has gained immense popularity by bridging the gap between dynamic JavaScript and static typing. Two of its fundamental features, “Type” and “Interface,” play pivotal roles in…

Leave a Reply

%d bloggers like this: