2017-12-01 11 views
1

Google 스크립트의 객체에서 findIndex()를 사용하려고하는데 작동하지 않습니다.Google 스크립트의 indexOf

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; 
} 

아니라 자바 스크립트는 Console하지만 구글 스크립트에서이 작품은 나에게 돌려 : 여기 exemple이다 "형식 오류를 : Fonction findIndex이 개체에서 찾을 수 없습니다 ....."

+0

Google 스크립트 란 무엇입니까? –

+0

어떤 브라우저를 사용하십니까? 'findIndex'는 IE에서 사용할 수없는 ES6 메소드입니다. – Barmar

+0

또한 'this'는 콜백 함수에서 작동하지 않습니다. – Barmar

답변

0

당신은 할 수 없습니다.

개체에 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} 

정확히 같은 객체. 따라서 찾을 수있는 색인이 없습니다.