For-each over an array in JavaScript
JavaScript
/** 1. Use forEach and related */
var a = ["a", "b", "c"];
a.forEach(function(entry) {
console.log(entry);
});
/** 2. Use a simple for loop */
var index;
var a = ["a", "b", "c"];
for (index = 0; index < a.length; ++index) {
console.log(a[index]);
}
/**3. Use for-in correctly*/
// `a` is a sparse array
var key;
var a = [];
a[0] = "a";
a[10] = "b";
a[10000] = "c";
for (key in a) {
if (a.hasOwnProperty(key) && // These checks are
/^0$|^[1-9]\d*$/.test(key) && // explained
key <= 4294967294 // below
) {
console.log(a[key]);
}
}
/** 4. Use for-of (use an iterator implicitly) (ES2015+) */
const a = ["a", "b", "c"];
for (const val of a) {
console.log(val);
}
/** 5. Use an iterator explicitly (ES2015+) */
const a = ["a", "b", "c"];
const it = a.values();
let entry;
while (!(entry = it.next()).done) {
console.log(entry.value);
}
Also in JavaScript:
- Title
- javascript character ascii value modify
- Category
- JavaScript
- Title
- how to update all node libraries
- Category
- JavaScript
- Title
- conditional rendering in react
- Category
- JavaScript
- Title
- if object is array javascript
- Category
- JavaScript
- Title
- async await anonymous function
- Category
- JavaScript
- Title
- injected stylesheet remove
- Category
- JavaScript
- Title
- event listener on parent not on childjavascript
- Category
- JavaScript
- Title
- Angular watching for changes in $http.pendingRequests from directive
- Category
- JavaScript
- Title
- get iso date javascript
- Category
- JavaScript
- Title
- get day js
- Category
- JavaScript
- Title
- capitalize first letter javascript
- Category
- JavaScript
- Title
- how to call action from another module vuex
- Category
- JavaScript
- Title
- adding pre tag javascript
- Category
- JavaScript
- Title
- how to iterate through an object in javascript
- Category
- JavaScript
- Title
- iframe reload parent page
- Category
- JavaScript
- Title
- how do I use const in javascript
- Category
- JavaScript
- Title
- array matching
- Category
- JavaScript
- Title
- function inside object javascript
- Category
- JavaScript
- Title
- emmet jsx visual studio
- Category
- JavaScript
- Title
- how to display image in html from json object
- Category
- JavaScript
- Title
- declare function javascript
- Category
- JavaScript
- Title
- how do you remove a remove element from array in javascript
- Category
- JavaScript
- Title
- Angular material design
- Category
- JavaScript
- Title
- curl post file json
- Category
- JavaScript
- Title
- html get elements by class
- Category
- JavaScript
- Title
- disable right click javascript
- Category
- JavaScript
- Title
- cb() never called! npm ERR! This is an error with npm itself.
- Category
- JavaScript
- Title
- gitignore for angular
- Category
- JavaScript
- Title
- angular two way property binding
- Category
- JavaScript
- Title
- google sign in in firebase react
- Category
- JavaScript
- Title
- get date now javascript
- Category
- JavaScript
- Title
- find leap year javascript
- Category
- JavaScript
- Title
- get row data in datatable
- Category
- JavaScript
- Title
- how to delete an element from an array in javascript
- Category
- JavaScript
- Title
- how to remove a property from an object in javascript
- Category
- JavaScript
- Title
- hwo to cehck req header in js
- Category
- JavaScript
- Title
- how to know if a number has a decimal number js
- Category
- JavaScript
- Title
- git remote url change
- Category
- JavaScript
- Title
- how to square a value in javascript
- Category
- JavaScript
- Title
- chart.js label word wrap
- Category
- JavaScript
- Title
- faker js
- Category
- JavaScript
- Title
- how to find id in array javascript
- Category
- JavaScript
- Title
- get url of page in background script
- Category
- JavaScript
- Title
- file input disable open file picker javascript
- Category
- JavaScript
- Title
- 404 json laravel
- Category
- JavaScript
- Title
- express bodyparser deprecated
- Category
- JavaScript
- Title
- discord js channel count
- Category
- JavaScript
- Title
- inline z-index react
- Category
- JavaScript
- Title
- concat object
- Category
- JavaScript
- Title
- get the state of a checkbox
- Category
- JavaScript
- Title
- addeventlistener on document in react js
- Category
- JavaScript
- Title
- convert dictionary to json serialization swift 4
- Category
- JavaScript
- Title
- fixed div with scrollable content
- Category
- JavaScript
- Title
- convert a int to a unicode javascript
- Category
- JavaScript
- Title
- add webpack to react project
- Category
- JavaScript
- Title
- how to add button in alert box in react native
- Category
- JavaScript
- Title
- javascript add scroll listener
- Category
- JavaScript
- Title
- function prototype javascript
- Category
- JavaScript
- Title
- access index of array javascript
- Category
- JavaScript
- Title
- connect nodejs to mariadb
- Category
- JavaScript
- Title
- assign freemarker expressions to variables
- Category
- JavaScript
- Title
- how to make a textarea unwritable in react native
- Category
- JavaScript
- Title
- display none in jquery
- Category
- JavaScript
- Title
- how to check string uppercase or lowersace using regex javascript
- Category
- JavaScript
- Title
- how to update node.js on linux
- Category
- JavaScript
- Title
- elasticsearch test node.js
- Category
- JavaScript
- Title
- discord.js multiple embeds
- Category
- JavaScript
- Title
- javascript add function to onchange event
- Category
- JavaScript
- Title
- ${ js
- Category
- JavaScript
- Title
- delete all childs in node
- Category
- JavaScript
- Title
- How to get the path to the file that required your module?
- Category
- JavaScript
- Title
- how to add multiple videos in html5 with javascript
- Category
- JavaScript
- Title
- bubble sort
- Category
- JavaScript
- Title
- array.splice javascript
- Category
- JavaScript
- Title
- go to another page javascript
- Category
- JavaScript
- Title
- how to add changes every time you route navigate to page in angular
- Category
- JavaScript
- Title
- filter array objects javascript
- Category
- JavaScript
- Title
- how to add row in angular dynamically
- Category
- JavaScript
- Title
- ascii to char in javascript
- Category
- JavaScript
- Title
- how to initialize an array in javascript
- Category
- JavaScript
- Title
- getboundingclientrect() javascript
- Category
- JavaScript
- Title
- filter out arrays js
- Category
- JavaScript
- Title
- how to manage a db connection in javascript
- Category
- JavaScript
- Title
- angular attach component to body
- Category
- JavaScript
- Title
- convert class object to json python
- Category
- JavaScript
- Title
- how to add class to element on mouseclick with js
- Category
- JavaScript
- Title
- create react app deployment heroku
- Category
- JavaScript
- Title
- deploy angular app on google app engine
- Category
- JavaScript
- Title
- addEventListener
- Category
- JavaScript
- Title
- custom status discord bot
- Category
- JavaScript
- Title
- hasownproperty.call
- Category
- JavaScript
- Title
- extend react.component
- Category
- JavaScript
- Title
- creating an object javascript
- Category
- JavaScript
- Title
- javascript array stackoverflow
- Category
- JavaScript
- Title
- how to get data from localstorage in javascript
- Category
- JavaScript
- Title
- get last item in array js
- Category
- JavaScript
- Title
- add a slash to string in javascript
- Category
- JavaScript
- Title
- function to create an element javascript
- Category
- JavaScript
- Title
- copy to clipboard javascript
- Category
- JavaScript
- Title
- get variable from other javascript file
- Category
- JavaScript