Sử dụng map() với destructuring để trích xuất trường name từ mảng đối tượng JSON.
Ví dụ
const studentDetails = [
{ name: "John" },
{ name: "David" },
{ name: "Bob" }
];
const studentNames = studentDetails.map(({name: actualValue}) => actualValue);
console.log(studentNames);
Cách chạy
node demo.js
Kết quả
[ 'John', 'David', 'Bob' ]
Cách đơn giản hơn:
const studentNames = studentDetails.map(item => item.name);
// Hoặc destructuring trực tiếp
const studentNames = studentDetails.map(({name}) => name);