다음이 우리의 객체라고 가정해 봅시다 -
var customerDetails=
[
{
customerId:101,
customerName:"John"
},
{
customerId:102,
customerName:"David"
},
{
customerId:103,
customerName:"Mike"
},
{
customerId:104,
customerName:"Bob"
}
] 홀수 CustomerID 레코드만 표시하려면 다음 조건과 함께 for 루프를 사용하십시오. -
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 -----------------------------------------------------------