【问题标题】:JS inserting objects correctlyJS正确插入对象
【发布时间】:2020-03-22 14:26:22
【问题描述】:

我正在尝试制作迷你口袋宠物小精灵游戏。这是我存储口袋妖怪的方式。

let Charmander = {
        "name": "Charmander",
        "type": "fire",
        "hp": "70",
        "weaknesses": ["ground", "rock", "water"],
    };
let Bulbasaur = {
        "name": "Bulbasaur",
        "type": ["Grass", "Poison"],
        "hp": "70",
        "weaknesses": ["Fire", "Flying", "Ice", "Psychic"],
    };
let Squirtle = {
        "name": "Squirtle",
        "type": "Water",
        "hp": "60",
        "weaknesses": ["Electric", "Grass"],
};

然后它会要求你选择你想要的起始口袋妖怪。

let startingChoice = prompt("What is your starting Pokemon " + Charmander.name + ", " + Bulbasaur.name + ", " + Squirtle.name);

然后我为第一个可以挑选的口袋妖怪创建一个数组

let firstPokemon = [ Charmander.name, Bulbasaur.name, Squirtle.name];

然后我会检查你输入的结果,看看它是否在里面。

if(startingChoice == "Charmander"){

    pokeBox.push(Charmander);

} else if(startingChoice == "Bulbasaur"){
    pokeBox.push(Bulbasaur);
}else{
    pokeBox.push(Squirtle);
}



}else{
    console.log("Chupa");
}

正如您在上面看到的那样,效率很低。我怎样才能做到这一点,以便当它放入您的 pokeBox 时,它会添加所有对象信息,而无需所有信息语句。

这是 pokeBox 的结果 Object { name: "Charmander", type: "fire", hp: "70", … } 我怎样才能使它更有效率?

【问题讨论】:

  • 使用单个对象,而不是许多单独的对象,使用名称作为键和信息对象作为值。然后只需使用输入的名称作为获取信息对象的键,例如pokemons[promptedName]

标签: javascript arrays object if-statement include


【解决方案1】:

你是如何偶然宣布 pokeBox 的?

    let starterKantoPokemon = [{
        "name": "Charmander",
        "type": "fire",
        "hp": "70",
        "weaknesses": ["ground", "rock", "water"],
    },
    {
        "name": "Bulbasaur",
        "type": ["Grass", "Poison"],
        "hp": "70",
        "weaknesses": ["Fire", "Flying", "Ice", "Psychic"],
    },
    {
        "name": "Squirtle",
        "type": "Water",
        "hp": "60",
        "weaknesses": ["Electric", "Grass"],
  }];
    let pokeBox = []; 

    let hasPokemonBeenChosen = false;
    while(!hasPokemonBeenChosen)
    {
        let startingChoice = prompt("What is your starting Pokemon\n 1. " + starterKantoPokemon[0].name + ", 2." + starterKantoPokemon[1].name + ", 3." + starterKantoPokemon[2].name);
       if(startingChoice >= 1 && startingChoice <=3) {
         pokeBox.push(starterKantoPokemon[startingChoice - 1]);
         hasPokemonBeenChosen = true;
       } 
}
console.log('Professor Oak: Hmm...' + pokeBox[0].name + '. Good choice!');

【讨论】:

    【解决方案2】:

    希望这会有所帮助。

        let pokeList = [{
            "name": "Charmander",
            "type": "fire",
            "hp": "70",
            "weaknesses": ["ground", "rock", "water"],
        }, {
            "name": "Bulbasaur",
            "type": ["Grass", "Poison"],
            "hp": "70",
            "weaknesses": ["Fire", "Flying", "Ice", "Psychic"],
        }, {
            "name": "Squirtle",
            "type": "Water",
            "hp": "60",
            "weaknesses": ["Electric", "Grass"],
    }]
    var name = "";
    var pokeName = pokeList.filter((p)=>{
        name += p.name+", ";
    })
    let startingChoice = prompt("What is your starting Pokemon " + name.slice(0, -2));
    
    var pokeBox = [];
    
    pokeList.forEach(function(item, index){
        if(startingChoice == item.name){
            pokeBox.push(item)
        }
    })
    
    console.log(pokeBox)
    

    【讨论】:

      【解决方案3】:

      关键是将所有的口袋妖怪放在自己的对象中。然后你可以用这样一种方式编码它,所有重要的是对象中的口袋妖怪,没有重复的名字或任何东西。在下面的示例中,您可以添加或删除您喜欢的任何口袋妖怪,它的工作方式相同。

      const
        pokemon = {
             charmander: {
                "name": "Charmander",
                "type": "fire",
                "hp": "70",
                "weaknesses": ["ground", "rock", "water"],
            },
            bulbasaur: {
                "name": "Bulbasaur",
                "type": ["Grass", "Poison"],
                "hp": "70",
                "weaknesses": ["Fire", "Flying", "Ice", "Psychic"],
            },
            squirtle: {
                "name": "Squirtle",
                "type": "Water",
                "hp": "60",
                "weaknesses": ["Electric", "Grass"],
            },
        },
        pokeBox = [],
        promptString = `What is your starting pokemon?\n` +
          Object.values(pokemon)
          .map(p => p.name)
          .join(`, `),
        choice = prompt(promptString);
          
      const lowerCaseChoice = choice.toLowerCase();
      if (lowerCaseChoice in pokemon)
          pokeBox.push(pokemon[lowerCaseChoice]);
      else
          console.log(`that is not a valid option`);
      
      console.log(pokeBox)

      【讨论】:

        【解决方案4】:

        希望这对你有用,这是一种有效的方法。

        let starterPokemons = {
               charmander: {
                  "name": "Charmander",
                  "type": "fire",
                  "hp": "70",
                  "weaknesses": ["ground", "rock", "water"],
              },
              bulbasaur: {
                  "name": "Bulbasaur",
                  "type": ["Grass", "Poison"],
                  "hp": "70",
                  "weaknesses": ["Fire", "Flying", "Ice", "Psychic"],
              },
              squirtle: {
                  "name": "Squirtle",
                  "type": "Water",
                  "hp": "60",
                  "weaknesses": ["Electric", "Grass"],
              },
          };
        let pokeBox = [];
        let promptQuestion = 'What is your starting Pokemon\n';
        Object.keys(starterPokemons).forEach(function(pokemon){
            promptQuestion += pokemon + ', ';
        });
        
        while(pokeBox.length == 0)
        {
            let startingChoice = prompt(promptQuestion.slice(0, -2));
            startingChoice = startingChoice.toLowerCase();
        
            if(starterPokemons[startingChoice] == undefined){
                alert("Invalid Starter Pokemon");
            }
            else{
                pokeBox.push(starterPokemons[startingChoice]);
                alert("Congratulations you have selected " + starterPokemons[startingChoice].name + " as your Starter Pokemon");
            }
        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-10-29
          • 2011-08-23
          • 2011-03-25
          • 1970-01-01
          • 2017-05-11
          • 1970-01-01
          • 2020-02-23
          相关资源
          最近更新 更多