To execute a Python enumerate function in JavaScript

Introduction

When developing a web application using Node.js, I was turning the list with forEach. I also needed index information in a hurry.

So I wrote the following source code, but my friends pointed out that there was a better way. When I actually tried it, it was very convenient and I felt that it was close to Python's enumerate function, so I will leave it in the article as a way to get the index and value at the same time with the ** for statement **.

ary = ["bar", "foo", "hoge"]

let i = 0 
ary.forEach(function( value ) {
     console.log(i, value );
     i++
});
// 0 bar
// 1 foo
// 2 hoge

Execution environment

Verification environment

What is the enumerate function in the first place?

A function that gets the index and value of the list specified in the argument.

ary = ["bar", "foo", "hoge"]
for i, item in enumerate(ary):
    print(i, item)
#  0 bar
#  1 foo
#  2 hoge

Get index and value at the same time with for statement

forEach can specify an index as the second argument

In the forEach function, the first argument is a list and the second argument is an index.

This index is automatically incremented as the number of loops increases, so You do not have to write i ++ or i + = 1.

ary = ["bar", "foo", "hoge"]
ary.forEach(function(value, i) {
     console.log(i, value);
});
// 0 bar
// 1 foo
// 2 hoge

Use for in

for in gets the index of the list. The index and value are retrieved by the following source code.

ary = ["bar", "foo", "hoge"]

for (i in ary){
    console.log(i, ary[i])
}
// 0 bar
// 1 foo
// 2 hoge

On the other hand, for in may show unexpected behavior as follows, so It seems that you need to be careful when using it.

var data = [ 'apple', 'orange', 'banana'];
//Added hoge method to array object
Array.prototype.hoge = function(){}
  for (var key in data){
   console.log(data[key]);
  }
// apple
// orange
// banana
// function(){}

reference

Recommended Posts

To execute a Python enumerate function in JavaScript
Create a function in Python
How to execute a command using subprocess in Python
To return char * in a callback function using ctypes in Python
python enumerate function
Precautions when pickling a function in python
[Introduction to Udemy Python3 + Application] 45. enumerate function
How to get a stacktrace in python
Scraping a website using JavaScript in Python
I wrote a function to load a Git extension script in Python
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
[Small story] A painstaking measure when you have to execute a function before import in Python
Draw a graph of a quadratic function in Python
Try to calculate a statistical problem in Python
Automatically register function arguments to argparse in Python
How to clear tuples in a list (Python)
How to embed a variable in a python string
Generator function in JavaScript
Get the caller of a function in Python
I want to create a window in Python
How to create a JSON file in Python
A clever way to time processing in Python
Steps to develop a web application in Python
To add a module to python put in Julialang
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to Mock a Public function in Pytest
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
Try to make a Python module in C language
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
[Road to Python Intermediate] Define __getattr__ function in class
[Python] How to expand variables in a character string
Create a plugin to run Python Doctest in Vim (2)
I tried to implement a pseudo pachislot in Python
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
I want to work with a robot in python.
Things to note when initializing a list in Python
Specifies the function to execute when the python program ends
Consider a conversion from a Python recursive function to a non-recursive function
Introduction to Linear Algebra in Python: A = LU Decomposition
[Python] Created a method to convert radix in 1 second
Execute Python function from Powershell (how to pass arguments)
[Python] How to call a c function from python (ctypes)
Publish / upload a library created in Python to PyPI
How to handle JSON in Ruby, Python, JavaScript, PHP
What does the last () in a function mean in Python?
Take a screenshot in Python
Covector to think in function
To flush stdout in Python
Create a dictionary in Python
A road to intermediate Python
Use callback function in Python
ntile (decile) function in python
Login to website in Python
How to call a function
Execute external command in python