对于循环未正确访问 JSON 对象

For loop not accessing JSON Objects properly?

本文关键字:访问 JSON 对象 于循环 循环      更新时间:2023-09-26

由于某种原因,当我完成测验时,无法访问正确的JSON对象属性。一旦字符串被识别出来,它就应该访问该属性。如果我硬编码它.[0]、[1] 等它有效。我该如何解决这个问题?另外,如何使此代码更简洁?我觉得除了使用这么多 if 语句之外,可能还有另一种方法。谢谢。

有问题的对象

var personTypes = [{ 
        type : "INTJ",
        typeInfo: "Imaginative and strategic thinkers, with a plan for everything.",
        },
        {type : "INTP",
        typeInfo: "Innovative inventors with an unquenchable thirst for knowledge.",
        },
        {type : "ENTJ",
        typeInfo: "Bold, imaginative and strong-willed leaders, always finding a way – or making one.",
        },
        {type : "ENTP",
        typeInfo: "Smart and curious thinkers who cannot resist an intellectual challenge.",
        },
        {type : "INFJ",
        typeInfo: "Quiet and mystical, yet very inspiring and tireless idealists",
        },
        {type : "INFP",
        typeInfo: "Poetic, kind and altruistic people, always eager to help a good cause.",
        },
        {type : "ENFJ",
        typeInfo: "Charismatic and inspiring leaders, able to mesmerize their listeners.",
        },
        {type : "ENFP",
        typeInfo: "Enthusiastic, creative and sociable free spirits, who can always find a reason to smile.",
        },
        {type : "ISTJ",
        typeInfo: "Practical and fact-minded individuals, whose reliability cannot be doubted.",
        },
        {type : "ISFJ",
        typeInfo: "Very dedicated and warm protectors, always ready to defend their loved ones.",
        },
        {type : "ESTJ",
        typeInfo: "Excellent administrators, unsurpassed at managing things – or people.",
        },
        {type : "ESFJ",
        typeInfo: "Extraordinarily caring, social and popular people, always eager to help.",
        },
        {type : "ISTP",
        typeInfo: "Bold and practical experimenters, masters of all kinds of tools.",
        },
        {type : "ISFP",
        typeInfo: "Flexible and charming artists, always ready to explore and experience something new.",
        },
        {type : "ESTP",
        typeInfo: "Smart, energetic and very perceptive people, who truly enjoy living on the edge",
        },
        {type : "ESFP",
        typeInfo: "Spontaneous, energetic and enthusiastic people – life is never boring around them.",
        },
];

访问对象

 // once done with quiz
   if (questionNum === 3) {
        //concat radio inputs
        var typeConcat = this.ei + this.sn+ this.tf+ this.pj;
        //output concat to screen
        $("p").show();
        $("h2").text("Your type is " + typeConcat);

            //use this data inside the <p>
        for (i = 0; i < personTypes.length; i++) {    
            if (typeConcat=="INTJ") {
                $("p").text(personTypes[i].typeInfo);
            }
            if (typeConcat=="INTP") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ENTJ") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ENTP") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="INFJ") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="INFP") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ENFJ") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ENFP") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ISTJ") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ISFJ") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ESTJ") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ESFJ") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ISTP") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ISFP") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ESTP") {
                $("p").text(personTypes[i].typeInfo);
            }
             if (typeConcat=="ESFP") {
                $("p").text(personTypes[i].typeInfo);
            }
        }
    }

我认为当你指定了类型时,你不需要所有的if语句,对吧?这将同样有效。

编辑:不要忘记在i = 0之前包含var

for (var i = 0; i < personTypes.length; i++) {    
    if (typeConcat==personTypes[i].type) {
        $("p").text(personTypes[i].typeInfo);
    }
}

更简洁的版本是将类型放在哈希图中,并通过给定的类型Concat访问类型:

var personTypes = {
  INTJ: "Imaginative and strategic thinkers, with a plan for everything.",
  INTP: "Innovative inventors with an unquenchable thirst for knowledge.",
  ENTJ: "Bold, imaginative and strong-willed leaders, always finding a way – or making one.",
  ENTP: "Smart and curious thinkers who cannot resist an intellectual challenge.",
  INFJ: "Quiet and mystical, yet very inspiring and tireless idealists",
  INFP: "Poetic, kind and altruistic people, always eager to help a good cause.",
  ENFJ: "Charismatic and inspiring leaders, able to mesmerize their listeners.",
  ENFP: "Enthusiastic, creative and sociable free spirits, who can always find a reason to smile.",
  ISTJ: "Practical and fact-minded individuals, whose reliability cannot be doubted.",
  ISFJ: "Very dedicated and warm protectors, always ready to defend their loved ones.",
  ESTJ: "Excellent administrators, unsurpassed at managing things – or people.",
  ESFJ: "Extraordinarily caring, social and popular people, always eager to help.",
  ISTP: "Bold and practical experimenters, masters of all kinds of tools.",
  ISFP: "Flexible and charming artists, always ready to explore and experience something new.",
  ESTP: "Smart, energetic and very perceptive people, who truly enjoy living on the edge",
  ESFP: "Spontaneous, energetic and enthusiastic people – life is never boring around them."
}
$("p").text(personTypes[typeConcat]);