Skip to content
Advertisement

Can ‘=’ be used to set one object property given a different unique key:value?

If I know an object exists in an array with a unique key:value pair do I have use .find() to get the object or is there a way that doesn’t require iteration?

Given:

JavaScript

Is there a notation to do:

JavaScript

Or do I have to do:

JavaScript

Advertisement

Answer

To directly answer your question…

Is there a notation to do

The answer is “no”.

If your elements have unique IDs, considering collecting them into a Map keyed by id if you need that sort of access…

JavaScript
JavaScript

Because the object references in testObj and the Map are the same, any changes to one will be reflected in the other.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement