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

111
Views
Find value in Javascript array

I'm struggling with a task in javascript. I want to extract the value from an array in an API. I want to get the last_price in the Monet_ADA array. Here is the data in the API.

641f0571d02b45b868ac1c479fc8118c5be6744ec3d2c5e13bd888b6.ZOMBIE_ADA: {last_price: 0.0016, base_volume: 0, quote_volume: 0} 682fe60c9918842b3323c43b5144bc3d52a23bd2fb81345560d73f63.NEWM_ADA: {last_price: 2.4e-8, base_volume: 0, quote_volume: 0} 722c45e8ba2a3c399cf09949abe74546ecb75defb8206914085dc28e.CDX_ADA: {last_price: 0.22, base_volume: 0, quote_volume: 0} 776da8416dc56107275424b009eb82353235f10ef8e0b77088fd7964.TrustCoin_ADA: {last_price: 0.00006, base_volume: 0, quote_volume: 0} 782c158a98aed3aa676d9c85117525dcf3acc5506a30a8d87369fbcb.Monet_ADA: {last_price: 1.1e-9, base_volume: 0, quote_volume: 0} 804f5544c1962a40546827cab750a88404dc7108c0f588b72964754f.VYFI_ADA: {last_price: 0.0000019, base_volume: 0, quote_volume: 0} 815f432f5d9b36e77989d0fea374292e9c8330c5146ca8500632b3cf.Squid_ADA: {last_price: 'NA', base_volume: 0, quote_volume: 0} 823f7c8e25ee35f368daf2c9b6b4783c1d9a763eb5227b50b095f6c2.Djedi_ADA: {last_price: 'NA', base_volume: 0, quote_volume: 0} 0864aa509385d78b9d83e8547424a055bb93e152a767383c6e0ea854.AINU_ADA: {last_price: 'NA', base_volume: 0, quote_volume: 0}

Here is my code:

const api_url = 'http://analytics.muesliswap.com/ticker';
    
     async function getMonetPrice(){
        const response = await fetch (api_url);
        const data = await response.json();
        console.log(data);
    }
    
    getMonetPrice(); 

My code doesn't throw an error, it just returns all the data in the API. I need help parsing through the data and then returning just the last_price value in the Monet.ADA array. Please and thank you.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  1. It's an object, not an array, but you can still iterate over it.

  2. Extract the key/value from each Object.entries, and check to see if the key endsWith "Monet_ADA".

In this example I'm passing in an endpoint, and a query object, to make life a little easier.

// Get the data
function getData(endpoint) {
  return fetch(endpoint);
}

// Accept the data, and a query object
function finder(data, { type, prop }) {

  // Iterate over the object entries and get the
  // key/value from each
  for (const [key, value] of Object.entries(data)) {

    // If the key ends with the query type
    // return the query prop value
    if (key.endsWith(type)) return value[prop];
  }

  // Otherwise return a default message
  return 'No value available';

}

// Accept an endpoint, and a query object,
// get the data, and parse it. Then call `finder`
// with the data, and the query
async function main(endpoint, query) {
  const response = await getData(endpoint);
  const data = await response.json();
  const lastPrice = await finder(data, query);
  console.log(lastPrice);
}

const endpoint = 'https://analytics.muesliswap.com/ticker';
const query = { type: 'Monet_ADA', prop: 'last_price' };

main(endpoint, query);

about 4 years ago · Juan Pablo Isaza Report

0

const apiURL = 'http://analytics.muesliswap.com/ticker';

// Returns false on failure, or price on success
const getMonetPrice = async (apiURL) => {
    try {
        const response = await fetch(apiURL);
        const { '782c158a98aed3aa676d9c85117525dcf3acc5506a30a8d87369fbcb.Monet_ADA': monetPrice } = await response.json();
        if (!monetPrice) {
            throw "Couldn't find monetPrice";
            return;
        }

        const { last_price } = monetPrice;
        if (!last_price) {
            throw "Couldn't find last_price";
            return;
        }
        
        return last_price;
    } catch (error) {
        return false;
    }
};
    
const run = async () => {
    const monetPrice = await getMonetPrice(apiURL);
    console.log('monetPrice', monetPrice);
};

run();
about 4 years ago · Juan Pablo Isaza Report

0

It looks like the data is an object with obscure keys, one of them ending in a string that the OP aims to get at. First find the key...

const key = Object.keys(data).find(k => k.endsWith('Monet_ADA'));
const lastPrice = key ? data[key].last_price : null;
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!