LoginSignup
2
5

More than 3 years have passed since last update.

javascript vue.js object 配列を検索

Posted at

vue.jsで配列を検索したい。
しかし、なぜか find を使うと googlebot が エラーを吐く。
これはSEO的にまずい。

ということで、 filter を使って要素を取得したり、存在チェックしたりします。

まずは元になるデータ


var arr = [
    {
        id:100,
        name: '一郎',
        age: 25
    },
    {
        id:102,
        name: '二郎',
        age: 21
    },
    {
        id:153,
        name: '三郎',
        age: 18
    }
];


ID153のデータはある?


var tmp = arr.filter(e => e.id == 153);

if(tmp.length > 0){
    console.log("ID153のデータはあります");
} else {
    console.log("ユーザーが見つかりません");
}


データを絞り込む


//指定した条件のデータを抽出
var tmp = arr.filter(e => e.age >= 20);

// 結果
// 0: {id: 100, name: "一郎", age: 25}
// 1: {id: 102, name: "二郎", age: 21}

2
5
1

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
2
5