สำหรับสิ่งนี้ ให้ใช้ Object.keys() เช่นเดียวกับ Object.values() และ map() สำหรับผลลัพธ์
ตัวอย่าง
const object = {
name: 'John',
age: 21,
countryName: 'US',
subjectName: 'JavaScript'
}
const allKeysOfObject = Object.keys(object);
console.log("The all keys are=" + allKeysOfObject);
const allValues = Object.values(object);
console.log("The all values are=" + allValues);
console.log("The use of map is as follows=");
allKeysOfObject.map(k => { console.log(object[k]) }) ในการรันโปรแกรมข้างต้น คุณต้องใช้คำสั่งต่อไปนี้ -
node fileName.js.
ที่นี่ ชื่อไฟล์ของฉันคือ demo185.js
ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
PS C:\Users\Amit\javascript-code> node demo185.js The all keys are=name,age,countryName,subjectName The all values are=John,21,US,Javascript The use of map is as follows= John 21 US JavaScript