Dave Appleton Dave Appleton - 9 days ago 6
Javascript Question

iterate though JSON array in p5.js

I have a server that returns :
JSON Image

I want to iterate through this data in p5.js

var ms = []

function preload() {
var url ='https://dest/url'
ms = loadJSON(url)
}


Which I expected to return an array but it does not seem to return anything sensible.

Chrome debug of JSON

However, if I paste the same data into the Javascript console I get different data :

pasted JSON

How do I either iterate over this data (it is NOT loaded as an array) or convert it to an array?

Answer

I think you mean this:

var array;
for (var key in ms) {
   if (!array) {
      array = [a[key]];
   } else {
      array.push(a[key]);
   }
}
console.log(array); // [ {'time': .... }, {...}, .. ]
Comments