Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Hiểu phương thức find() để tìm bản ghi cụ thể - JavaScript


Dùng find() với điều kiện để lấy bản ghi cụ thể.

Ví dụ

Mã thực hiện:

var obj=[ 
  { studentId:101, studentName:"John" }, 
  { studentId:102, studentName:"Bob" }, 
  { studentId:103, studentName:"David" } 
] 
const result = obj.find( 
  (o) => { 
    return o.studentId === 102 
  } 
); 
console.log(result);

Chạy lệnh:

node fileName.js.

Tên file: demo315.js.

Kết quả

Đầu ra:

PS C:\Users\Amit\javascript-code> node demo315.js 
{ studentId: 102, studentName: 'Bob' }