2017-12-01 5 views
1

GoogleスクリプトのオブジェクトでfindIndex()を使用しようとしていますが、動作しません。googleスクリプトのindexOf

+0

Googleスクリプトとは –

+0

あなたはどのブラウザを使用していますか? 'findIndex'はES6メソッドです。IEでは利用できません。 – Barmar

+0

また、 'this'はコールバック関数では機能しません。 – Barmar

答えて

0

はあなたがすることはできません:「..... Fonction findIndexオブジェクト内に見つからない例外TypeError」この作品は、JavaScriptのコンソルでよく

function myFunction() { 
var searchingJohn = "John"; 
var DATA = {namesList: []}; 
DATA.namesList.push({name: "John", otherDataForThisName: []}); 
var result = DATA.namesList.findIndex(function (element) 
     {return element.name == this;}, searchingJohn); 
return result; 
} 

が、Googleのスクリプトは私を返します。ここでexempleです。

オブジェクトにfindIndex()を使用しようとしていますが、findIndex()は配列用です。 MDNのドキュメントから

findIndex()上:

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.

理由は、オブジェクトが特定の順序でキーと値のペアを保持していないということです。例えば、

{key1:value1, key2:value2} 

{key2:value2, key1:value1} 

は全く同じオブジェクトです。したがって、見つかるインデックスはありません。