0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

findメソッド、filterメソッドの違い

Posted at

##findメソッド
条件に一致した最初の項目を取得

var users = [
  { id: 1, admin: false },
  { id: 2, admin: false },
  { id: 3, admin: true },
  { id: 4, admin: false },
  { id: 5, admin: true },

];

var admin = users.find((user) => {
    return user.admin === true;
})

admin; // { id: 3, admin: true }

##filterメソッド
配列から条件に一致した全ての項目を取得

var users = [
 { id: 1, admin: true },  
 { id: 2, admin: false },
 { id: 3, admin: false },
 { id: 4, admin: false },
 { id: 5, admin: true },
];

var filteredUsers = users.filter((user) => {
    return  user.admin === true;
});


filteredUsers; //[{"id":1,"admin":true},{"id":5,"admin":true}]

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?