---
title: "サポートされていないJavaScript関数と代替"
slug: "unsupported-javascript-functions-and-alternatives"
abstract: "Sitecore CDPでサポートされていないJavaScript関数の概要(代替JavaScript付き)。"
---
# サポートされていないJavaScript関数と代替

このトピックでは、Sitecore CDPでサポートされていないJavaScript関数と、代わりに使える代替機能について説明します。

以下のJavaScriptエンティティはSitecore CDPでサポートされておらず、代替例は提供していません。

-   async 機能
-   generator 機能
-   map primitive 機能
-   promiseオブジェクト
-   データセットprimitiveデータ型
-   supportシンボルを一意識別子として
-   weakmap データ構造
-   weakset データ構造
-   オプションのチェーン演算子(?.)
-   ヌリッシュコイレスシン演算子(??)

## Array.entries

Sitecore CDPはArray.entries関数をサポートしていません。以下のJavaScriptを使って、配列内のキー/値ペアを順番に切り替えられる新しいアレイイテレーターオブジェクトを返してください。

var a, ...b = 1, 2, 3;
print(a); // 1
print(b); // 2, 3

## Array.find

Sitecore CDPはArray.find関数をサポートしていません。代替として、以下のJavaScriptを使って配列内の各要素をチェックできます。コードが真の値を持つ配列要素を見つけた場合、その配列要素の値を返します。

// 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 = arguments1;
 var value;

 for (var i = 0; i < length; i++) {
 value = listi;
 if (predicate.call(thisArg, value, i, list)) {
 return value;
 }
 }
 return undefined;
 };
}

## Array.findIndex

Sitecore CDPはArray.findIndex関数をサポートしていません。代替として、以下のJavaScriptを使って真の値を返す配列要素をチェックできます。コードが真の値を返す配列要素を見つけた場合、その配列要素のインデックスを返します。残りの値はチェックしません。

// 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 = arguments1;
 var value;

 for (var i = 0; i < length; i++) {
 value = listi;
 if (predicate.call(thisArg, value, i, list)) {
 return i;
 }
 }

 return -1;
 };
 }

## Array.flat

以下のJavaScriptを使って、多次元配列を単一次元配列に変換します。特定のシナリオでは*reduce*や*spread* 、または分解構文を使うことができます(コードサンプルに示されています)。Sitecore CDPはArray.flat関数をサポートしていません。

// 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(arguments0) ? 1 : Number(arguments0);

 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

以下のJavaScriptを使って、入力配列要素を新しい配列にフラット化します。コードサンプルに示されているように *、特定の状況ではreduce*を使うことができます。Sitecore CDPはArray.flatMap関数をサポートしていません。

// 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

以下のJavaScriptを使って、配列に指定された要素が含まれているかどうかを判定します。コードは配列にその要素が含まれている場合はtrueを返し、含まれていなければfalseを返します。Sitecore CDPArray.includes関数をサポートしていません。

// Use indexOf with +1 to allow easy boolean check
const array1 = ""a"", ""b"", ""c"";
if(array1.indexOf(""a"")+1) {
 // do something
}

## Array.keys

以下のJavaScriptを使って配列のキーまたはキーの一部を返します。Sitecore CDPはArray\_keys関数をサポートしていません。

// 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

以下のJavaScriptを使って配列のスライスメソッドを使い、呼び出しでオブジェクトに適用してください。Sitecore CDPArray.of関数をサポートしていません。

// Polyfill
if (!Array.of) {
 Array.of = function() {
 return Array.prototype.slice.call(arguments);
 // Or
 let vals = ;
 for(let prop in arguments){
 vals.push(argumentsprop);
 }
 return vals;
 }
}

## Array.values

以下のJavaScriptを使って配列のすべての要素を返してください。Sitecore CDPArray.values関数をサポートしていません。

// Polyfill
if (!.values) {
 Array.prototype.values = function() {
 var k, a = , nextIndex = 0, ary = this;
 k = ary.length;
 while (k > 0) a--k = aryk;
 a.next = function(){
 return nextIndex < ary.length ?
 {value: arynextIndex++, done: false} :
 {done: true};
 };
 return a;
 };
}

## なぜ...の

以下のJavaScriptを使って反復可能なオブジェクトを反復処理してください。Sitecore CDPはfor...of文をサポートしていません。

const array1 = 'a', 'b', 'c';
array1.forEach(value => {
 print(value)
});

## Object.assign

以下のJavaScriptを使って、1つまたは複数のソースオブジェクトからターゲットオブジェクトに値をコピーします。Sitecore CDPはObject.assign関数をサポートしていません。

// 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 = argumentsindex;

 if (nextSource !== null && nextSource !== undefined) {
 for (var nextKey in nextSource) {
 // Avoid bugs when hasOwnProperty is shadowed
 if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
 tonextKey = nextSourcenextKey;
 }
 }
 }
 }
 return to;
 },
 writable: true,
 configurable: true
 });
}

## Object.enentry

以下のJavaScriptを使って、指定されたオブジェクトの列挙可能な文字列キー付きプロパティkey, value配列を、定義方法に関係なく提供された順序で返します。Sitecore CDPはObject.entries関数をサポートしていません。

// 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 = argumentsindex;

 if (nextSource !== null && nextSource !== undefined) {
 for (var nextKey in nextSource) {
 // Avoid bugs when hasOwnProperty is shadowed
 if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
 tonextKey = nextSourcenextKey;
 }
 }
 }
 }
 return to;
 },
 writable: true,
 configurable: true
 });
}

## Object.js

以下のJavaScriptを使って、2つの値が同じかどうかを判断します。Sitecore CDPはObject.js関数をサポートしていません。

// 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

以下のJavaScriptを使って、与えられたオブジェクトの列挙可能なプロパティ値の配列を返します。Sitecore CDPはObject.values関数をサポートしていません。

// 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) ? Ok : ), );
 };
}