Javascript in server side script


(Sang) #1

**Matrix Version:v5.5.1.3

var testObj = {
  '2000': 'Articel1',
  '4000': 'Articel2',
  '1000': 'Articel3',
  '3000': 'Articel4',
};

// I'll explain what reduces does after the answer.
console.log(Object.keys(testObj).reduce((accumulator, currentValue) => {
  accumulator[currentValue] = testObj[currentValue];
  return accumulator;
}, {}));

Object.keys are not working in server-side script?
I found sort is not working as well.


(Jsisk) #2

Hi there,

Unfortunately, what you can do with JS depends on the version of the JavaScript engine that Matrix is running.
In your case, Matrix 5.5.1.3, it would be running on PHP 5.4, which is tied to an older version of the V8JS, so some of the new JS functions and methods might not be available to you.

Ideally, you could arrange to see if you could get upgraded to a newer version of Matrix, which would have a newer version of PHP to take advantage of the latest features and bug/security fixes.

In the meantime, I am trying to find out which version of the the V8JS engine your systems support so that you can see what you are working with.

I’ll let you know how I go


#3

You could try the polyfill if you really need that method

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#polyfill