for (var key in validation_messages) {
// skip loop if the property is from prototype
if (!validation_messages.hasOwnProperty(key)) continue;
var obj = validation_messages[key];
for (var prop in obj) {
// skip loop if the property is from prototype
if (!obj.hasOwnProperty(prop)) continue;
// your code
alert(prop + " = " + obj[prop]);
}
}
Answer from AgileJon on Stack OverflowMDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › entries
Object.entries() - JavaScript | MDN
// Using for...of loop const obj = { a: 5, b: 7, c: 9 }; for (const [key, value] of Object.entries(obj)) { console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" } // Using array methods Object.entries(obj).forEach(([key, value]) => { console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" }); Polyfill of Object.entries in core-js ·
Top answer 1 of 16
2265
for (var key in validation_messages) {
// skip loop if the property is from prototype
if (!validation_messages.hasOwnProperty(key)) continue;
var obj = validation_messages[key];
for (var prop in obj) {
// skip loop if the property is from prototype
if (!obj.hasOwnProperty(prop)) continue;
// your code
alert(prop + " = " + obj[prop]);
}
}
2 of 16
924
Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():
var obj = {
first: "John",
last: "Doe"
};
//
// Visit non-inherited enumerable keys
//
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
Videos
04:40
4 Easy Ways to Loop Through Objects in JavaScript - YouTube
11:21
Easy Ways to Loop Over Objects in JavaScript - YouTube
06:33
How to Use JavaScript forEach Loop | Complete Tutorial with Examples ...
08:02
JavaScript forEach() method in 8 minutes! ➿ - YouTube
How to use JavaScript foreach method | How to use forEach ...
06:16
How to iterate through an object in JavaScript - YouTube
Google
developers.google.com › google workspace › apps script › create a mail merge with gmail & google sheets
Create a mail merge with Gmail & Google Sheets | Apps Script | Google for Developers
* @param {string} subject_line to search for draft message * @return {object} containing the subject, plain and html message body and attachments */ function getGmailTemplateFromDrafts_(subject_line) { try { // get drafts const drafts = GmailApp.getDrafts(); // filter the drafts that match subject line const draft = drafts.filter(subjectFilter_(subject_line))[0]; // get the message object const msg = draft.getMessage(); // Handles inline images and attachments so they can be included in the merge // Based on https://stackoverflow.com/a/65813881/1027723 // Gets all attachments and inline image
Laravel
laravel.com › docs › 12.x › validation
Validation | Laravel 12.x - The clean stack for Artisans and agents
For more information on working with this object, check out its documentation. So, in our example, the user will be redirected to our controller's create method when validation fails, allowing us to display the error messages in the view: ... <!-- /resources/views/post/create.blade.php --> <h1>Create Post</h1> @if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <!-- Create Post Form -->
Reddit
reddit.com › r/javascript › is object.keys(obj).foreach really any better than for-in loop?
r/javascript on Reddit: Is Object.keys(obj).forEach really any better than for-in loop?
February 1, 2017 -
In Douglas Crockford's talk at Nordicjs in 2014 he says that he now prefers this syntax over for-in:
Object.keys(obj).forEach(function(key) { /* ... */ });However jsperf says it's slower and every performance metric I can find says it's slower. Did I miss anything here? I wonder why Crockford prefers it...
Top answer 1 of 5
25
Yes, there is a difference. Object.keys() iterates over "own" properties, while for-in iterates over all enumerable properties, even those inherited in the prototype. Also, speed really isn't a concern for nearly all use cases.
2 of 5
14
I think the takeaway here is more the Object.keys/Object.values/Object.entries pattern because it returns an Array which can be used with all your favorite methods like map, filter, reduce, forEach, etc. It's a single pattern to remember, it's easy to use and is heavily supported within the JS ecosystem and has no nasty surprises.
Chart.js
chartjs.org › docs › latest › samples › other-charts › doughnut.html
Doughnut | Chart.js
const DATA_COUNT = 5; const NUMBER_CFG = {count: DATA_COUNT, min: 0, max: 100}; const data = { labels: ['Red', 'Orange', 'Yellow', 'Green', 'Blue'], datasets: [ { label: 'Dataset 1', data: Utils.numbers(NUMBER_CFG), backgroundColor: Object.values(Utils.CHART_COLORS), } ] }; const actions = [ { name: 'Randomize', handler(chart) { chart.data.datasets.forEach(dataset => { dataset.data = Utils.numbers({count: chart.data.labels.length, min: 0, max: 100}); }); chart.update(); } }, { name: 'Add Dataset', handler(chart) { const data = chart.data; const newDataset = { label: 'Dataset ' + (data.datasets
Mastering JS
masteringjs.io › tutorials › fundamentals › foreach-object
Iterating Through an Object with `forEach()` - Mastering JS
May 29, 2020 - You can use `forEach()` to iterate over a JavaScript object using `Object.key()`, `Object.values()`, and `Object.entries()`. Here's what you need to know.
React
react.dev › learn › thinking-in-react
Thinking in React – React
function ProductCategoryRow({ category }) { return ( <tr> <th colSpan="2"> {category} </th> </tr> ); } function ProductRow({ product }) { const name = product.stocked ? product.name : <span style={{ color: 'red' }}> {product.name} </span>; return ( <tr> <td>{name}</td> <td>{product.price}</td> </tr> ); } function ProductTable({ products }) { const rows = []; let lastCategory = null; products.forEach((product) => { if (product.category !== lastCategory) { rows.push( <ProductCategoryRow category={product.category} key={product.category} /> ); } rows.push( <ProductRow product={product} key={product.name} /> ); lastCategory = product.category; }); return ( <table> <thead> <tr> <th>Name</th> <th>Price</th> </tr> </thead> <tbody>{rows}</tbody> </table> ); } function SearchBar() { return ( <form> <input type="text" placeholder="Search..."
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › forEach
Array.prototype.forEach() - JavaScript | MDN
The forEach() method of Array instances executes a provided function once for each array element.
Mastering JS
masteringjs.io › tutorials › fundamentals › foreach-key-value
How to Use forEach() with Key Value Pairs - Mastering JS
After that, you can then use forEach() to iterate through the keys, values, or entries: const obj = { name: 'Jean-Luc Picard', rank: 'Captain' }; // Prints "name Jean-Luc Picard" followed by "rank Captain" Object.keys(obj).forEach(key => { console.log(key, obj[key]); });
W3Schools
w3schools.com › jsref › jsref_foreach.asp
JavaScript Array forEach() Method
The forEach() method is not executed for empty elements.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › keys
Object.keys() - JavaScript | MDN
Object.keys() returns an array whose elements are strings corresponding to the enumerable string-keyed property names found directly upon object. This is the same as iterating with a for...in loop, except that a for...in loop enumerates properties in the prototype chain as well.
Latenode
latenode.com › home › blog › development & programming › javascript for automation › how to iterate over a javascript object?
How to Iterate Over a JavaScript Object? - Latenode Blog
February 12, 2026 - The for...in loop is a traditional way to iterate over over object javascript. It is simple and widely supported but requires an additional hasOwnProperty() check to avoid iterating over inherited properties. The Object.entries() method, combined with map() or forEach(), provides a concise way to access both keys and values of an object.
Useaxentix
useaxentix.com › blog › javascript › how-to-use-foreach-for-object-in-javascript
How to Use forEach for Object in JavaScript: Complete Guide
October 10, 2025 - JavaScript's forEach() method is a powerful array method that allows you to iterate over array elements, but it cannot be used directly on objects.
Altcademy
altcademy.com › blog › how-to-loop-through-object-in-javascript
How to loop through object in JavaScript
June 9, 2023 - Now that we understand what an object is, let's learn how to loop through its properties. Looping through an object means iterating over its properties one by one, usually to perform some operation on the property values or keys. There are several ways to do this in JavaScript, and we'll explore them one by one.
freeCodeCamp
freecodecamp.org › news › javascript-foreach-how-to-loop-through-an-array-in-js
JavaScript forEach – How to Loop Through an Array in JS
July 6, 2020 - The forEach method passes a callback function for each element of an array together with the following parameters: Current Value (required) - The value of the current array element · Index (optional) - The current element's index number · Array (optional) - The array object to which the current element belongs
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-iterate-over-object-properties-in-typescript
How to Iterate Over Object Properties in TypeScript - GeeksforGeeks
October 1, 2024 - Object.keys(object).forEach(key => { console.log(`${key}: ${object[key]}`); }); Example: In this example, we used assertion as Array<keyof typeof user> so that TypeScript knows that the keys are valid for the user object. JavaScript ·