Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
Running a conditional for my front-end application to return a string if the value of a property returns null

Below is my code. I have already ran "musical" through a console log and can confirm it is returning my state data. My goal is because not every object that has "buy-price" has a value to it(some return null as the value), I want to run this conditional where it can give a string that alerts users "this is not for sale" if null, or return the correct price if it does exist in the API.

import React, { useEffect, useState } from "react";
import fishes from "../APIs/animalCrossing";

export default function Music() {
  const [music, setMusic] = useState([]);

  useEffect(() => {
    fishes.getVillagerMusic().then((response) => {
      setMusic(response);
    });
  }, []);

  const buyMusic = () => {
    music.map((musical) => {
      if (musical["buy-price"] == null) {
        return "This Song is not for purchase";
      } else {
        return musical["buy-price"];
      }
    });
  };
  return (
    <div>
      {music.map((music, index) => {
        return (
          <div key={index}>
            <img src={music.image_uri} alt="music" />
            <p> Sold at: ${buyMusic()} </p>
          </div>
        );
      })}
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda