วิธีทำ
ใช้ logical NOT (!) operator ใน if condition เพื่อข้ามค่าที่ไม่ต้องการ
ตัวอย่างโค้ด
var customerDetails=[
{ customerName:"John", customerAge:28, customerCountryName:"US" },
{ customerName:"David", customerAge:25, customerCountryName:"AUS" },
{ customerName:"Mike", customerAge:32, customerCountryName:"UK" }
]
for(var i=0;i<customerDetails.length;i++){
if(customerDetails[i].customerCountryName!="AUS"){
console.log("The country name is="+customerDetails[i].customerCountryName);
}
}
วิธีรัน
node fileName.js
ชื่อไฟล์ demo179.js
ผลลัพธ์
PS C:\Users\Amit\javascript-code> node demo179.js The country name is=US The country name is=UK
คำอธิบาย
ใช้ != (not equal) ในเงื่อนไข if เพื่อข้าม object ที่ country เป็น "AUS" แสดงเฉพาะ US และ UK