
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Iteration of for loop inside object in JavaScript to fetch records with odd CustomerId?
Let’s say the following is our object −
var customerDetails= [ { customerId:101, customerName:"John" }, { customerId:102, customerName:"David" }, { customerId:103, customerName:"Mike" }, { customerId:104, customerName:"Bob" } ]
Use for loop with the following condition to display only odd CustomerID records −
for(var index=0;index<customerDetails.length;index++){ if(customerDetails[index].customerId % 2 !=0){ // } }
Example
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(""); }
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo71.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo71.js Customer Id=101 Customer Name=John ----------------------------------------------------------- Customer Id=103 Customer Name=Mike -----------------------------------------------------------
- Related Articles
- Why is using “for…in” loop in JavaScript array iteration a bad idea?
- Fetch elements of Java TreeSet using Iteration
- How can we fetch alternate odd numbered records from MySQL table?
- MySQL query for text search with LIKE and OR to fetch records
- PHP Object Iteration
- JavaScript variables declare outside or inside loop?
- Setting property in an empty object using for loop in JavaScript.
- Implement MySQL REGEXP to fetch records with . and numbers
- Why is using “for…in” with array iteration a bad idea in javascript?
- How to handle exception inside a Python for loop?
- Fetch maximum individual marks for a student with marks1 and marks2 records in MySQL?
- MySQL RegExp to fetch records with only a specific number of words
- Explain for. . .of loop JavaScript.
- MySQL REGEXP to fetch string + number records beginning with specific numbers?
- Using aggregation pipeline to fetch records in MongoDB

Advertisements