Ask Your Question
1

How to save json data obtained through a fetch request in one or several arrays, which can later be utilized in Javascript?

asked 2022-05-09 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-03-19 08:00:00 +0000

pufferfish gravatar image

To save JSON data obtained through a fetch request, you can follow these steps:

  1. Initialize an empty array to store the data:
let dataArray = [];
  1. Make the fetch request and convert the response to JSON format:
fetch('url')
  .then(response => response.json())
  .then(data => {
    // code to store data in arrays
  });
  1. Iterate through the data and push each object into the array:
data.forEach(item => {
  dataArray.push(item);
});

Alternatively, you can use the spread operator to add the data to the array:

dataArray = [...dataArray, ...data];
  1. Use the array(s) as needed in your JavaScript code.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-09 11:00:00 +0000

Seen: 7 times

Last updated: Mar 19 '22