*Have you ever come across situations where you need to dynamically assign values to object properties * or perform computations based on dynamic p...
For further actions, you may consider blocking this person and/or reporting abuse
The example for "dynamic property names" is spurious and would never be done in practice: as already noted you should just use a static property name for such a generic property - i.e.
isAdult
.I'm actually finding it very hard to find a practical example where this functionality might be needed; but I think I might have used it once or twice in the past 😅
You could perhaps use it to create a condensed data structure that is intended to be iterated with Object.entries()...
Both the map and reduce examples above are written in a condensed style I usually avoid since it hurts readability. For the reduce function I'd normally opt for "dynamic property assignment" since IMO that's clearer. i.e.:
Dynamic property assignment is something I have definitely needed more frequently; but it's still something of a rarity.
or maybe we can use Map() data strucure
To achieve what?
instead of using dynamic property
In what context?
TBH it looks like you had some help writing the article; so I'm not sure what you're trying to achieve here 😅
insted of using normal object we can use Map() insted If I need to use local variable or something like this . what you use insted of
we can use
is this works??
when you mentioned computed properties I thought it was about object getters hehe, for what you did I would just call it a dynamic object key.
computed properties would be something like this (in my understanding):
I think you are correct. I just learned from course computed but then I saw people call it
dynamic object key
to be honest this name makes more senseIf I need to use local variable or something like this . what you use insted of
[event.target.name]: value
In my experience this could be good for when you have to send custom events to an analytics platform such as Google Analytics or for simplifying the way an object is transformed - I came across somewhere I think this might have been useful where data was fetched from an api, modified by the user, then sent back to a different api that needed a different data structure. You never need it until you need it!
should I use Map() data strucure then ?
I'm still learning, can you tell me more, what is the best solution ?
say Hi
The example seems a little weird to me. Usually you report an analytics event by name, like this:
one thing to consider with dynamic keys too is the fact that it is a little harder to provide typescript types, meaning that if not done correctly, people will need to explore the code to understand what can be accessed.
In the "person" example that was provided in the post, this is how it would be typed:
if you are not using typescript then it will be harder for others to know that the "person" has a "isAdult" property