简体   繁体   中英

How to format Javascript Fetch API output

The REST API that I'm using returns data in the following format:

data:1
data:2
data:3

I would like to display the output in the following format instead:

123

This is how I'm making an API call using Javascript Fetch (with test url as an example):

let url = "https://jsonplaceholder.typicode.com/todos/" // test url
  fetch(url)
    .then(response => response.json())
    .then(data => {
      let htmlOutput = document.getElementById("htmlOutput");
      // parsing the JSON value to string
      htmlOutput.value = JSON.stringify(data);
    })

How can I format the output that I get when I make a call to an API, the way I want?

htmlOutput.value = data.map(e => e.data).join('');

 const data = [{ data: 1 }, { data: 2 }, { data: 3 }] const result = data.map(e => e.data).join(''); console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM