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

115
Vistas
Fetch data in only one key object

I have an array of objects stored in a currentCoins state as follows:

const [currentCoins, setCurrentCoins] = useState([])
    
console.log(currentCoins)

[
    {
        "name": "algorand",
        "quantity": 1,
        "currentPrice": 0
    },
    {
        "name": "ethereum",
        "quantity": 9,
        "currentPrice": 0
    },
    {
        "quantity": 4,
        "name": "bitcoin",
        "currentPrice": 0
    }
]

I would like to add a dynamic value from an API in my currentPrice, but I'm not sure how to go about and organize this.

I had thought of something like this but it's incomplete, and I don't know how to pass the name to the API

useEffect(() => {
    setCurrentCoins(coinsCopy) // update currentCoins array from another array that can be updated by the user 

    const fetchPrices = async (name) => {
      await fetch(
        `https://api.coingecko.com/api/v3/coins/${name}`
      )
       // this endpoint returns the current price to res.market_data.current_price.usd

        .then((res) => res.json())
        .then((res) => {
          // processing here?
        });
    };
    fetchPrices();
  }, [coins])

Processing should be added to the "// processing here?comment or am I going in the wrong direction?

The goal would be for this to override the "0" value of currentPrice for each of my objects.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

useEffect(() => {
    setCurrentCoins(coinsCopy) // update currentCoins array from another array that can be updated by the user 
    fetchPrices();
  }, [coins])


    const fetchPrices = async () => {
      var copyArray = [...currentCoins];
      for(let c of copyArray ){
          var res = await fetch(
           `https://api.coingecko.com/api/v3/coins/${c.name}`
           )
          if(res.ok){
           var data = await res.json()
           var currentPriceUSD = data.market_data.current_price.usd
           c.currentPrice =  currentPriceUSD 
          }else{
            console.log("Cannot find the coin");
          }
   }
      setCurrentCoins(copyArray)

    };
about 4 years ago · Juan Pablo Isaza Denunciar
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