2016-09-12 9 views
-3

私は以下のような配列を持っています。 this.itemsgroupcustomer.indexOf("Perorangan")を使用している場合、-1を返します。なぜこれが間違っているのか分かりません。助けてください。{nativescript} indexOf配列

viewModel.itemsgroupcustomer = [ 
     {title: "Perusahaan"}, 
     {title: "Perorangan"} 
    ]; 
+0

アレイには、 'Perorangan'文字列の要素が含まれていないためです。 – zerkms

答えて

3

ですfindIndexメソッドを使用する -

viewModel.itemsgroupcustomer.findIndex(x=>x.title==='Perorangan'); 
+0

Yups @chetan、これはもっと簡単です。ありがとう:) –

2

使用Array.prototype.find代わりに、それはテストに合格する要素を返します。

あなたはその配列

にそのインデックスを見つけるために、戻り値を使用することができます

const arr = [ 
 
    {title: "Perusahaan"}, 
 
    {title: "Perorangan"} 
 
] 
 
console.log(arr.find(el => el.title === 'Perorangan'))

const arr = [ 
 
    {title: "Perusahaan"}, 
 
    {title: "Perorangan"} 
 
] 
 
const filteredElement = arr.find(el => el.title === 'Perorangan') 
 
console.log(arr.indexOf(filteredElement))


UPDATE:ユーザー@zerkmsとして

があると指摘している単一のステップで上記を行う方法で構築され、それはArray.prototype.findIndex

const arr = [ 
 
    {title: "Perusahaan"}, 
 
    {title: "Perorangan"} 
 
] 
 
console.log(arr.findIndex(el => el.title === 'Perorangan'))

+1

そこに 'Array.prototype.findIndex'があります – zerkms

+0

ありがとう@mauricio、どのようにこの配列のものは動作しますか?時々私は配列の世界で失われています。ここに私のオンラインコード 'this.itemsgroupcustomer.indexOf(this.itemsgroupcustomer.find(el => el.title ===" Perorangan "))'があります。それはリターン1です。yay –

関連する問題