1

How to call data from this array?? I have to add two values from others urls. enter image description here

<table id="sales_table">
  <thead>
    <tr id="sales_item_title"></tr>
  </thead>
  <tbody>
    <tr id="sales_item"></tr>
  </tbody>
</table>
<script>
  async function init(){
  try{
      const results = await Promise.all([

        fetch('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=PLN').then((response)=> response.json()),

        fetch('https://min-api.cryptocompare.com/data/all/coinlist')
        .then((response)=> response.json()),

      ].map(promise=>promise.catch(error=>console.error)));

      console.log(results);
      document.querySelector('#sales_item').innerHTML = results.0.PLN

    } catch (error) {
      console.error(error);
    }
  }
  init();
</script>
2
  • Does this answer your question? How to access a element in JavaScript array? Commented Apr 15, 2022 at 9:02
  • @Siguza Sorry, but I want to display data from both API and add them. They are called 0 and 1, and i dont know how to call them in document.querySelector('#sales_item').innerHTML = results.data([1][0]).PLN Commented Apr 15, 2022 at 9:07

1 Answer 1

1

You return two objects and you have access to the object by key. In your example PLN . Then use : object.PLN.

async function init() {
    const first = await fetch('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=PLN')
    .then((response)=> response.json());

    const second = await fetch('https://min-api.cryptocompare.com/data/all/coinlist')
     .then((response)=> response.json());

    let merged = {...first, ...second};
    console.log(merged);    
    console.log('PLN:',merged.PLN)
}

init()
    .catch(e => {
        console.log('some problems: ' + e.message);
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.