pwshub.com

Setting Dynamic Objects Keys in JavaScript

Setting Dynamic Objects Keys in JavaScript

It's always a good day when you get pleasantly surprised by JavaScript, even more so when you find that it's a feature that's been around for a while (ES6, which according to Google was standardized in June of 2015). Earlier today I saw some syntax that didn't look quite right to me. Here's a simple example of it:

let type = 'name';
let person = {
    [type]:'Ray'
}

Specifically, the thing that surprised me was this portion:

[type]:'Ray'

If you console.log the code above, you get:

{ name: 'Ray' }

And then it makes sense. This syntax allows you to set a dynamic key in an object literal, much like:

person[type] = 'Ray';

Apparently, this has been around for nearly ten years and I never noticed it. Or, more likely, maybe I saw it and it didn't click in my head what was going on.

Officially you can refer to this as 'Computed keys in object literals' and can read more in Dr. Axel's Exploring JS book here: 30.7.2 Computed keys in object literals

Thanks to Dr. Axel, Tane Piper, and Caleb for all chiming in on Mastodon when I asked about this.

Support this Content!

If you like this content, please consider supporting me. You can become a Patron, visit my Amazon wishlist, or buy me a coffee! Any support helps!

Want to get a copy of every new post? Use the form below to sign up for my newsletter.

Source: raymondcamden.com

Related stories
4 days ago - Bringing data to life in your application can be done without the usual headaches. Paul Scanlon shows you how you can build beautiful data visualizations using the Google Analytics API, and you won’t have to spend any time “massaging” the...
1 month ago - This isn’t another AI-generated blog post about generic security practices. It contains detailed instructions on protecting Node.js applications...
1 month ago - Explore the challenges of manually fetching data from external APIs and how to easily do so using the Hygraph CMS. The post How to fetch data from external APIs using Astro and Hygraph appeared first on LogRocket Blog.
1 month ago - Learn how to use Tools with Twilio's AI Assistants and call trusted external data sources for better quality responses.
3 weeks ago - Postman is a popular API development and testing tool that allows developers to test, debug, and document APIs. Despite its popularity, Postman’s limited automation and collaboration capabilities, high pricing, and steep learning curve...
Other stories
1 hour ago - Hello, everyone! It’s been an interesting week full of AWS news as usual, but also full of vibrant faces filling up the rooms in a variety of events happening this month. Let’s start by covering some of the releases that have caught my...
2 hours ago - Nitro.js is a solution in the server-side JavaScript landscape that offers features like universal deployment, auto-imports, and file-based routing. The post Nitro.js: Revolutionizing server-side JavaScript appeared first on LogRocket Blog.
2 hours ago - Information architecture isn’t just organizing content. It's about reducing clicks, creating intuitive pathways, and never making your users search for what they need. The post Information architecture: A guide for UX designers appeared...
2 hours ago - Enablement refers to the process of providing others with the means to do something that they otherwise weren’t able to do. The post The importance of enablement for business success appeared first on LogRocket Blog.
3 hours ago - Learn how to detect when a Bluetooth RFCOMM serial port is available with Web Serial.