私は以下のような配列を持っています。 this.itemsgroupcustomer.indexOf("Perorangan")
を使用している場合、-1を返します。なぜこれが間違っているのか分かりません。助けてください。{nativescript} indexOf配列
viewModel.itemsgroupcustomer = [
{title: "Perusahaan"},
{title: "Perorangan"}
];
私は以下のような配列を持っています。 this.itemsgroupcustomer.indexOf("Perorangan")
を使用している場合、-1を返します。なぜこれが間違っているのか分かりません。助けてください。{nativescript} indexOf配列
viewModel.itemsgroupcustomer = [
{title: "Perusahaan"},
{title: "Perorangan"}
];
ですfindIndexメソッドを使用する -
viewModel.itemsgroupcustomer.findIndex(x=>x.title==='Perorangan');
Yups @chetan、これはもっと簡単です。ありがとう:) –
使用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'))
そこに 'Array.prototype.findIndex'があります – zerkms
ありがとう@mauricio、どのようにこの配列のものは動作しますか?時々私は配列の世界で失われています。ここに私のオンラインコード 'this.itemsgroupcustomer.indexOf(this.itemsgroupcustomer.find(el => el.title ===" Perorangan "))'があります。それはリターン1です。yay –
アレイには、 'Perorangan'文字列の要素が含まれていないためです。 – zerkms