Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!