What is a method (or combination of methods) in Javascript that will iterate over the elements of an array and, aside operating on the elements of the array, will also allow me to use the index number of the current element?
In ruby this would equate to something like this:
array.each_with_index{ |element,index| element.method(index) }
For arrays([1, 2, 3]
) you can use .forEach
array.forEach(function (element, index) {
});
for Objects
({a: 1, b: 2, c: 3}
) you can use .forEach
with combination Object.keys
Object.keys(obj).forEach(function (key) {
var value = obj[key];
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With