fujitsu fiji
fujitsu fiji

Reputation: 97

object keys values are undefined even though they are truthy

I want to check if the persona objects are falsy or truthy and depending on that log the values of the object keys into the console or displaying an error warning. In this example the persona object keys values are all truthy but when I log them into the console undefined is displayed, why?

let persona = {
  generalThings: {
    name: "Fijiwa",
    age: 22,
    birthday: "01.01.2000",
    placeOfBirth: "Tokyo",
    degree: "undefined",
    programLanguage: "JavaScript"
  },

  favouriteThings: {
    musicArtist: "Drake",
    book: "Meditations",
    person: "k",
    fighter: "Nate Diaz"
  },

}

foo = () => {
  for (key in persona) {
    const areTruthy = Object.values(persona[key]).every(value => value);
    console.log(areTruthy === true ? persona[key].value : "please complete the empty fields ");
  }
}
foo();

Upvotes: 0

Views: 544

Answers (1)

Valentine
Valentine

Reputation: 325

The first key is generalThings which is an object, so persona['generalThings'] is equal to

{
    name:"Fijiwa",
    age: 22,
    birthday: "01.01.2000",
    placeOfBirth: "Tokyo",
    degree: "undefined",
    programLanguage: "JavaScript" 
}

This object doesnot have a value property which means persona['generalThings'].value is undefined. I guess you want to log persona[key]

Upvotes: 2

Related Questions