Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the for in statement of JavaScript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use the for in sentence of JavaScript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the for in sentence of JavaScript.

The JavaScript for in statement loops through the properties of the object:

Grammar

For (key in object) {

/ / code block to be executed

}

Example

Const person = {fname: "John", lname: "Doe", age:25}

Let text = ""

For (let x in person) {

Text + = person [x]

}

Example explanation

For in loops through person objects

Returns one key per iteration (x)

The value of the key used to access the key

The value of the key is person [x]

For In traversal array

The JavaScript for in statement can also traverse the properties of the array:

Grammar

For (variable in array) {

Code

}

Example

Const numbers = [45, 4, 9, 16, 25]

Let txt = ""

For (let x in numbers) {

Txt + = numbers [x]

}

If the index order is important, do not use for in on the array.

The indexing order depends on the implementation, and the array values may not be accessed in the order you expect.

When order is important, it's best to use for loops, for of loops, or Array.forEach ().

Array.forEach ()

The forEach () method calls the function (callback function) once for each array element.

Example

Const numbers = [45, 4, 9, 16, 25]

Let txt = ""

Numbers.forEach (myFunction)

Function myFunction (value, index, array) {

Txt + = value

}

Note that the function takes three parameters:

Project value

Project index

The array itself

The above example uses only the value parameter. It can be rewritten as:

Example

Const numbers = [45, 4, 9, 16, 25]

Let txt = ""

Numbers.forEach (myFunction)

Function myFunction (value) {

Txt + = value

}

At this point, I believe you have a deeper understanding of "how to use the for in statement of JavaScript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report