Max not printing Javascript arrays

James Anderson's icon

James Anderson

4月 08 2024 | 8:16 午後

Hi everyone

I'm fairly new to using Max, and am very new to using Javascript with Max. My eventual goal is to treat lists as arrays in JS so that they can be modified and outputted as new lists. Unfortunately, I'm struggling with the basics!

This code works in JS:

function list(listIn) {

var array1 = listIn.map((x) => x * 2);

console.log(array1);

}

list([1, 2, 3, 4]);

Logged: '[ 2, 4, 6, 8 ]'

This code doesn't work in Max when sending the message '1 2 3 4':

function list() {

var array1 = arguments.map((x) => x * 2);

post(array1);

}

Logged: 'js: no function list [script name]'

I'm sure it must be very simple but I can't work it out. Thanks in advance!

tyler mazaika's icon

tyler mazaika

4月 09 2024 | 1:55 午前

Try the max JS function arrayfromargs() around your “listin” variable.

James Anderson's icon

James Anderson

4月 09 2024 | 5:12 午後

Thanks for the tip, Tyler. With arrayfromargs() I'm able to print the array without any modifications, but it still doesn't work when I attempt to put a calculation in the mix. Neither of these sets of code have worked:

function list() {

var array1 = arrayfromargs(arguments).map((x) => x * 2);

post(array1);

}

function list(listIn) {

var array1 = arrayfromargs(listIn).map((x) => x * 2);

post(array1);

}

Logged: '1 2 3 4'

Any ideas?

tyler mazaika's icon

tyler mazaika

4月 10 2024 | 3:33 午前

Ah, .map() may not be in available JS ES5, which is the version Max uses.

James Anderson's icon

James Anderson

4月 10 2024 | 9:36 午後

.map() is available in ES5, but the problem turned out to be something else that isn't: arrow functions. So thanks for the clue!

For anybody reading in the future, I got the code working with a different function syntax:

function list() {

var array1 = arrayfromargs(arguments).map(function(x) {

return (x * 2); })

post(array1);

}