Home > database >  Fetching api returns Array of Arrays instead Array of Objects
Fetching api returns Array of Arrays instead Array of Objects

Time:04-09

I am a Front end developer and I am working on a project which requires CRUD operations, I came to the point in which I have to fetch data from an API. I tested the API in Postman and Arc, the data is coming out fine as it should (Array of objects), but when I fetch in the app either with Axios or with Fetch the data I get is, Array of Arrays, this never happened and I could use the help.

axios
.get(
  "***$format=json",
  { withCredentials: "true" }
)
.then((data) => {
  console.log(data, "data");
})
.catch((err) => console.log(err, "err"));

Console log

enter image description here

CodePudding user response:

I have seen the consol log and it seems that it is an Array of Object only but the length of the array is 1823 so chrome changed the way of showing, it just made groups of 100 arrays so if you just try to iterate through the response using .map method then you will find out that actually, it is an array of object

CodePudding user response:

axios .get( "***$format=json", { withCredentials: "true" } ).then((data) => { console.log(data.d.results); }).catch((err) => console.log(err, "err"));

CodePudding user response:

It seems that if the response can be set from the backend, or if the backend does send the response in the form of an array of objects, you can convert to an array of array like this:

const output = input.map((obj) => Object.keys(obj).map((key) => obj[key]));
  • Related