Antoni Parellada
Antoni Parellada

Reputation: 4791

How to find basic properties of an object, e.g. array, in p5.js?

Incredibly I can't find an answer to this very essential tool to debug.

Say that I have created an index, but I am unsure it properly represents the pixels of a canvas. What command can I run to find summary features of this object, such as: array, integer valued, min, max, length, first 5 - 10 entries etc.

Upvotes: 0

Views: 317

Answers (1)

Paul Wheeler
Paul Wheeler

Reputation: 20140

There's no special p5.js support for helping you debug your JavaScript. You can either use an IDE such as Visual Studio Code with browser debugging configured, or you can use the browser's developer tools to inspect local variables or example them via the JavaScript console, having logged them via console.log(obj) as suggested in the comments. In some environments (such as openprocessing.org or editor.p5js.org) the print() function can be used to display debug information, so if you wanted to inspect the contents of an object without using the developer tools you could convert the object to JSON and display it with print(). Example:

let obj;

function setup() {
  noCanvas();
  obj = {
    foo: "bar",
    ary: [ 1, 1, 2, 3, 5 ]
  };
}

function mouseClicked() {
  print(JSON.stringify(obj));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

However, note that in a normal webpage the values passed to print() would only be visible in the browser's JavaScript console. Also not all objects can be converted to JSON strings.

Upvotes: 1

Related Questions