Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i read a property of an object in javascript

objects [ {
firstName: "Kristian",
lastName: "Vos",
number: "unknown",
likes: ["JavaScript", "Gaming", "Foxes"],
}….]

if this is my object, i need to read the word "firstName" and not the value of "firstName"

i know how to output the value of firstName ( objects[0].fisrtName ) = "Kristian"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

but what i really need to read is if the object has a property called firstName, in case that the object doesnt have that property.

thanks

>Solution :

I think you code isn’t actually object since you use [ ], it is more likely an array contain object.

This code should work, if you mistype the [ ], just use Object.keys(test)[0] instead.

let test = [{
  firstName: "Kristian",
  lastName: "Vos",
  number: "unknown",
  likes: ["JavaScript", "Gaming", "Foxes"]
}]
console.log(Object.keys(test[0])[0])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading