Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

สร้างอาเรย์จากข้อมูล JSON ด้วย map() ใน JavaScript

ใช้ map() ดึง Property ที่ต้องการจากอาเรย์อ็อบเจ็กต์ สร้างอาเรย์ใหม่

ตัวอย่างโค้ด

const studentDetails = [
    { name: "John" },
    { name: "David" },
    { name: "Bob" }
];

const listOfStudentName = studentDetails.map(({name: actualValue}) => actualValue);
console.log(listOfStudentName);

วิธีรันโปรแกรม

ใช้คำสั่ง node fileName.js (เช่น node demo82.js)

ผลลัพธ์

[ 'John', 'David', 'Bob' ]