Ruby 的 .each_with_index 的 JS 版本

JS version of Ruby's .each_with_index

Javascript 中的什么方法(或方法组合)将遍历数组的元素,除了对数组的元素进行操作外,还允许我使用索引号当前元素的?

在 ruby 中,这相当于这样的事情:

array.each_with_index{ |element,index| element.method(index) }

对于数组 ([1, 2, 3]) 你可以使用 .forEach

array.forEach(function (element, index) {

});

对于 Objects ({a: 1, b: 2, c: 3}) 您可以将 .forEachObject.keys

组合使用
Object.keys(obj).forEach(function (key) {
   var value = obj[key];
});