LoginSignup
4
1

More than 5 years have passed since last update.

Javascriptでオブジェクトの配列から特定の値が配列の何番目かを取得する

Posted at
var dessertList = [
    { id:1, name:"クッキー" },
    { id:2, name:"パンケーキ" },
    { id:3, name:"チョコレート" },
    { id:4, name:"アイスクリーム" },
    { id:5, name:"マシュマロ" },
];

var num;
for(var i = 0; i < dessertList.length; i++) {
    if(dessertList[i].name == "チョコレート") {
        num = i;
    }
}
console.log(num); // 2が返ってくる

ページングの処理を書くときに必要だったので、
実装してみました。

4
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1