+ 2
Calling object properties by string
Hello! I have following code: const obj = { a: "blablabla", b: "bloblo", c: "ble" }; I want to get an property of this object by typing its name: const get = "a"; console.log(obj[get]); It works, but my question is: is that a proper way to get object properties? I have read in many courses that this way of calling is proper for calling array elements, but not objects properties. Object properties we get from this syntax: object.property. Thank you in advance for anserws, bye!
4 Respuestas
+ 1
thank you for answer, but i need to call it by string variable (from user input for example)
+ 1
because i can't call it by obj.get in my example, i always need obj[get]
+ 1
Ok i attached my code.
https://code.sololearn.com/Wo3QW12tNGQJ/?ref=app
0
thank you, you make it clear to me :-)