-1
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"

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

MWO
  • 2,627
  • 2
  • 10
  • 25

3 Answers3

1

You can check

Object.keys(objects[0])

Or you can just check it like that

if ('firstName' in objects[0]) {
  // ...
}
gkucmierz
  • 1,055
  • 1
  • 9
  • 26
0

You can achieved this using Object.keys():

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Nathan Wiles
  • 841
  • 10
  • 30
0

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])
James
  • 2,732
  • 2
  • 5
  • 28