मान लें कि निम्नलिखित हमारा उद्देश्य है -
var customerDetails=
[
{
customerId:101,
customerName:"John"
},
{
customerId:102,
customerName:"David"
},
{
customerId:103,
customerName:"Mike"
},
{
customerId:104,
customerName:"Bob"
}
] केवल विषम CustomerID रिकॉर्ड प्रदर्शित करने के लिए निम्न शर्त के साथ लूप के लिए उपयोग करें -
for(var index=0;index<customerDetails.length;index++){
if(customerDetails[index].customerId % 2 !=0){
//
}
} उदाहरण
var customerDetails=
[
{
customerId:101,
customerName:"John"
},
{
customerId:102,
customerName:"David"
},
{
customerId:103,
customerName:"Mike"
},
{
customerId:104,
customerName:"Bob"
}
]
for(var index=0;index<customerDetails.length;index++){
if(customerDetails[index].customerId % 2 !=0){
console.log("Customer Id="+customerDetails[index].customerId);
console.log("Customer
Name="+customerDetails[index].customerName);
console.log("---------------------------------------------------
--------")
}
console.log("");
} उपरोक्त प्रोग्राम को चलाने के लिए, आपको निम्न कमांड का उपयोग करने की आवश्यकता है -
node fileName.js.
यहाँ, मेरी फ़ाइल का नाम है demo71.js.
आउटपुट
यह निम्नलिखित आउटपुट देगा -
PS C:\Users\Amit\JavaScript-code> node demo71.js Customer Id=101 Customer Name=John ----------------------------------------------------------- Customer Id=103 Customer Name=Mike -----------------------------------------------------------