How to set dynamic keys on objects in javascript #shorts

preview_player
Показать описание
My VSCode Extensions:
- theme: material community high contrast
- fonts: Menlo, Monaco, 'Courier New', monospace
- errors: Error Lens
- extra git help: Git Lens
- tailwind css intellisense
- indent rainbow
- material icon theme
- prettier & eslint
- ES7+ React Snippets

------------

Рекомендации по теме
Комментарии
Автор

Bro I was waiting for something clever it’s a variable lmao

maxprehoda
Автор

Awww here I thought we'd see some clever trick to generate dynamic keys

hikari
Автор

Speaking as a full-time developer myself, this trick is a really dangerous double-edged sword. Unless you are certain on the shape of final object, this habit of mutation object structure will eventually get yourself into the hell of "cannot get property 'something' of undefined" down the stream when trying consuming such unknown typed objects later on in the code.
A proper way to do this is to use the Map type, so that you can properly do typing on both the key and value, ensuring that the next person/function consuming the data object will know exactly what they could expect from the object

MIO_sh
Автор

yup also we can do in main object
const key = "name";
const obj = { [key]: "hello world" }

EngineBoy
Автор

If you dont understand u can use vars as keys you should probably go back a bit

BaxzXD
Автор

You should show how to create a hashmap with this concept. Would be much easier for beginners. I learned it the hard way.

kris
Автор

That's so crazy. I had to look this exact thing up at work today and now I see this video right before bed. Wild

TheChiefwiggy
Автор

when i learned about this, i realized that the arrays in javascript are simply objects with only number properties ( also "length" property and a bunch of methods).

Thialeth
Автор

Dynamic keys in typescript:
Interface Bob {
[key: string]: string;
}

thenewgeneration
Автор

WTF was that. I thought there was some kind of cool logic involved. Got pranked 😂

watchrandomstuffs
Автор

I love your videos and I use them everyday when writing js

princecharles-amachree
Автор

Cool trick to work with objects in JavaScript 😎.
Thanks for the tip 🍉😉👍!

dcknature
Автор

I really like your JavaScript Shorts, they are a brilliant way for me to learn new concepts that I didn't know existed. Please keep posting

mulwelimushiana
Автор

Wow! This particular problem took me a whole day looking for alternative because I don't know how to do this

arinzeezeozue
Автор

In those times I am grateful that I switched to typescript

NicolasVycasNery
Автор

Wow thats epic your a really good coder bro 👏 👍 👌

ttrss
Автор

My guy failed to tell you the purpose of this feature so I will.
This syntax allows you to have properties with illegal characters: myvar["my:property"] = "test"
You do also need to use this syntax when working with Symbol.

Momo-vyxw
Автор

Last week I did something similar. I had to keep track of some data so that I don't have to hit the api on every click. rn I am having a really weird satisfying feeling XD

facelessvoid
Автор

This is because JavaScript objects are actually just hash tables

bashmohandes
Автор

Most of the time, I suggest not mutating a variable or an object. It's much better to just declare it beforehand as const.

const obj = {[key]: "value"}

EldorJ
join shbcf.ru