Sometimes, we want to add dynamic key-value pairs to a JavaScript array or hash table.
In this article, we’ll look at how to add dynamic key-value pairs to a JavaScript array or hash table.
How to add dynamic key-value pairs to a JavaScript array or hash table?
To add dynamic key-value pairs to a JavaScript array or hash table, we can use computed key names.
For instance, we write
const obj = {};
obj[name] = val;
to add a property with the value of name
string as the property name.
We assign val
to as the property’s value.
Conclusion
To add dynamic key-value pairs to a JavaScript array or hash table, we can use computed key names.