简体   繁体   English

object 键值未定义,即使它们是真实的

[英]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.我想检查角色对象是虚假的还是真实的,并根据该记录将 object 键的值记录到控制台或显示错误警告。 In this example the persona object keys values are all truthy but when I log them into the console undefined is displayed, why?在此示例中,角色 object 键值都是真实的,但是当我将它们登录到控制台时显示未定义,为什么?

 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();

The first key is generalThings which is an object, so persona['generalThings'] is equal to第一个键是 generalThings,它是一个 object,所以 persona['generalThings'] 等于

{
    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.这个 object 没有value属性,这意味着 persona['generalThings'].value 是未定义的。 I guess you want to log persona[key]我猜你想登录 persona[key]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM