tengo esta matriz..
const colours = { normal: '#A8A77A', fire: '#EE8130', water: '#6390F0', electric: '#F7D02C', grass: '#7AC74C', ice: '#96D9D6', fighting: '#C22E28', poison: '#A33EA1', ground: '#E2BF65', flying: '#A98FF3', psychic: '#F95587', bug: '#A6B91A', rock: '#B6A136', ghost: '#735797', dragon: '#6F35FC', dark: '#705746', steel: '#B7B7CE', fairy: '#D685AD', };y luego tengo una variable que toma el tipo de pokeapi. Estoy sentado aquí tratando de escribir una sentencia if/else que básicamente dice
if (type === Object.keys(colours).name) { body.style.background = (El valor compartido de object.keys. Entonces, si está conectado a tierra, este valor debería ser #E2BF65.
¿Algún consejo sobre cómo puedo manejar este desafío?
Podrías escribir directamente algo como:
backgroundColor = colours[type] ? colours[type] : white Entonces, si colours[type] no están undefined , el color de fondo será white ; de lo contrario, el color del tipo que pasó.
Pruebe de esta manera para hacer referencia a un valor de objeto.
const colours = { normal: '#A8A77A', fire: '#EE8130', water: '#6390F0', electric: '#F7D02C', grass: '#7AC74C', ice: '#96D9D6', fighting: '#C22E28', poison: '#A33EA1', ground: '#E2BF65', flying: '#A98FF3', psychic: '#F95587', bug: '#A6B91A', rock: '#B6A136', ghost: '#735797', dragon: '#6F35FC', dark: '#705746', steel: '#B7B7CE', fairy: '#D685AD', }; let typeFromPoke = "normal"; result = colours[typeFromPoke] || null; console.log(result); typeFromPoke = "steel"; result = colours[typeFromPoke] || null; console.log(result); typeFromPoke = "somethingelse"; result = colours[typeFromPoke] || null; console.log(result);