Unsupported JavaScript functions and alternatives
This topic describes the JavaScript functions that are not supported in Sitecore Personalize and the alternative functions you can use instead.
The following JavaScript entities are not supported in Sitecore Personalize and we do not provide alternative examples:
-
async
functions -
generator
functions -
map primitive
functions -
promise
objects -
set
primitive
data types -
support
symbols as unique identifiers -
weakmap
data structure -
weakset
data structure
Array.entries
Sitecore Personalize does not support the Array.entries
function. Use the following JavaScript to return a new array iterator object that enables you to iterate through the key/value pairs in the array.
var [a, ...b] = [1, 2, 3];
print(a); // 1
print(b); // [2, 3]
Array.find
Sitecore Personalize does not support the Array.find
function. As an alternative, you can use the following JavaScript to check for each element present in the array. If the code finds an array element with a true value, it returns the value of that array element.
// Polyfill array.find
if (!Array.prototype.find) {
Array.prototype.find = function(predicate) {
if (this === null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
};
}
Array.findIndex
Sitecore Personalize does not support the Array.findIndex
function. As an alternative, you can use the following JavaScript to check for an array element that returns a true value. If the code finds an array element with a true value, it returns the index of that array element. It does not check the remaining values.
// Polyfill array.findIndex
if (!Array.prototype.findIndex) {
Array.prototype.findIndex = function(predicate) {
if (this === null) {
throw new TypeError('Array.prototype.findIndex called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return i;
}
}
return -1;
};
}
Array.flat
Use the following JavaScript to take a multidimensional array and turn it into a single dimensional array. You can use reduce or spread in certain scenarios or a decomposition syntax, as shown in the code sample. Sitecore Personalize does not support the Array.flat
function.
// Possible to use reduce or spread for some cases
const array = [1, 2, [3, 4]];
array.reduce((acc, val) => acc.concat(val), []);
// [1, 2, 3, 4]
// or with decomposition syntax
const flattened = array => [].concat(...array);
// Polyfill for more complex/nested
if (!Array.prototype.flat) {
Object.defineProperty(Array.prototype, 'flat', {
configurable: true,
value: function flat () {
var depth = isNaN(arguments[0]) ? 1 : Number(arguments[0]);
return depth ? Array.prototype.reduce.call(this, function (acc, cur) {
if (Array.isArray(cur)) {
acc.push.apply(acc, flat.call(cur, depth - 1));
} else {
acc.push(cur);
}
return acc;
}, []) : Array.prototype.slice.call(this);
},
writable: true
});
}
Array.flatMap
Use the following JavaScript to flatten the input array element into a new array. You can use reduce in certain scenarios, as shown in the code sample. Sitecore Personalize does not support the Array.flatMap
function.
// Use reduce for some cases
var array = [1, 2, 3, 4];
array.reduce((acc, x) => acc.concat([x, x * 2]), []);
// [1, 2, 2, 4, 3, 6, 4, 8]
// Polyfill more complex cases
if (!Array.prototype.flatMap) {
Object.defineProperty(Array.prototype, 'flatMap', {
configurable: true,
value: function flatMap (callback) {
return Array.prototype.map.apply(this, arguments).flat();
},
writable: true
});
}
Array.includes
Use the following JavaScript to determine whether an array contains a specified element. The code returns true
if the array contains the element, and false
if not. Sitecore Personalize does not support the Array.includes
function.
// Use indexOf with +1 to allow easy boolean check
const array1 = [""a"", ""b"", ""c""];
if(array1.indexOf(""a"")+1) {
// do something
}
Array.keys
Use the following JavaScript to return the keys or a subset of the keys of an array. Sitecore Personalize does not support the Array_keys
function.
// Polyfill
if (![].keys) {
Array.prototype.keys = function() {
var k = 0, o = this;
return {
next: function(){
return k < o.length ?
{value: k++, done: false} :
{done: true};
}
};
};
}
Array.of
Use the following JavaScript to use the slice method of an array and apply it toward an object using call. Sitecore Personalize does not support the Array.of
function.
// Polyfill
if (!Array.of) {
Array.of = function() {
return Array.prototype.slice.call(arguments);
// Or
let vals = [];
for(let prop in arguments){
vals.push(arguments[prop]);
}
return vals;
}
}
Array.values
Use the following JavaScript to return all the elements of the array. Sitecore Personalize does not support the Array.values
function.
// Polyfill
if (![].values) {
Array.prototype.values = function() {
var k, a = [], nextIndex = 0, ary = this;
k = ary.length;
while (k > 0) a[--k] = ary[k];
a.next = function(){
return nextIndex < ary.length ?
{value: ary[nextIndex++], done: false} :
{done: true};
};
return a;
};
}
For...of
Use the following JavaScript to iterate over iterable objects. Sitecore Personalize does not support the for...of
statement.
const array1 = ['a', 'b', 'c'];
array1.forEach(value => {
print(value)
});
Object.assign
Use the following JavaScript to copy the values from one or more source objects to a target object. Sitecore Personalize does not support the Object.assign
function.
// Polyfill
if (typeof Object.assign !== 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, ""assign"", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target === null || target === undefined) {
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource !== null && nextSource !== undefined) {
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
Object.entries
Use the following JavaScript to return an array of a given object's own enumerable string-keyed property [key, value]
pairs, in the same order as provided, regardless of how an object is defined. Sitecore Personalize does not support the Object.entries
function.
// Polyfill
if (typeof Object.assign !== 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, ""assign"", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target === null || target === undefined) {
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource !== null && nextSource !== undefined) {
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
Object.js
Use the following JavaScript to determine whether two values are the same. Sitecore Personalize does not support the Object.js
function.
// Polyfill
if (!Object.is) {
Object.is = function(x, y) {
if (x === y) { // Steps 1-5, 7-10
return x !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
};
}
Object.values
Use the following JavaScript to return the array of the given object's enumerable property values. Sitecore Personalize does not support the Object.values
function.
// Polyfill
const reduce = Function.bind.call(Function.call, Array.prototype.reduce);
const isEnumerable = Function.bind.call(Function.call, Object.prototype.propertyIsEnumerable);
const concat = Function.bind.call(Function.call, Array.prototype.concat);
const keys = Reflect.ownKeys;
if (!Object.values) {
Object.values = function values(O) {
return reduce(keys(O), (v, k) => concat(v, typeof k === 'string' && isEnumerable(O, k) ? [O[k]] : []), []);
};
}