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

What are the methods of traversing arrays with es6

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

Share

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

This article mainly shows you the "es6 traversal array what are the methods", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the methods of es6 traversal array" this article.

The method of es6 traversing the array: 1, use forEach (), you can call a function for each element in the array; 2, use map (), call the specified callback function for each element of the array; 3, use filter (); 4, use some (); 5, use every (); 6, use reduce ().

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

Common array traversal (iterative) methods in ES6

ForEach

Map

Filter

Some

Every

Reduce

Declare an object that needs to be iterated

The following code refers to this object

Let data = {code: 1, list: [{id: 23, title: "Women's wear 1", price: 300}, {id: 24 Title: "Women's Wear 2", price: 200}, {id: 27, title: "Menswear 1", price: 100} {id: 29, title: "Menswear 2", price: 400}, {id: 230, title: "Women's Wear 3" Price: 600}, {id: 40, title: "Children's Wear 1", price: 700}]} forEach

You can't use break and continue statements in forEach.

/ / there are two parameters, the first parameter is a numeric value, the second parameter is the index value data.list.forEach (function (item,index) {console.log (item,index) / / the output result is {/ / {id: 23, title: "women's wear 1", price: 300} 0 id / {id: 24, title: "women's wear 2", price: 200} 1 move / {id: 27, title: "men's wear 1" Price: 100} 2 id / {id: 29, title: "Menswear 2", price: 400} 3 shock / {id: 230, title: "Women's Wear 3", price: 600} 4 shock / {id: 40, title: "Children's Wear 1" Price: 700} 5Compact /}) map / / map maps / / traverses the data and returns a new array. The processing of the data returns the original corresponding position let arr = [2,3,6] Let newArr = arr.map (function (val, index) {/ / the first parameter is the value and the second parameter is the index value console.log (arr)})

* * traversing the data and returning a new array the processing of the data will return to the original corresponding position

To add a code block map cannot parse the same block-level scope

{} {} represents different block-level scopes and writes in different areas * *

/ / shallow copy / / shallow copy means that a gives the value to b when the value of b changes the value of a b at the same time. {let arr = [2, 3, 6] Let newArr = arr.map (function (index) Val) {/ / the first parameter is the index value, the second parameter is the value}) console.log (arr) / / 0: 2 / / 1: 3 / / 2: 6} {/ / shallow copy / / shallow copy means that a gives the value to b When the value of b changes the value of a b changes at the same time. Let newArr = data.list.map ((item, index) = > {item.price = item.price * .6 return item;}) Console.log (newArr) / / the price of the printed result is changed. Same {/ / 0: {id: 23, title: "Women's Wear 1", price: 180} / / 1: {id: 24, title: "Women's Wear 2", price: 120} / / 2: {id: 27, title: "Menswear 1", price: 60} / / 3: {id: 29, title: "Menswear 2", price: 240} / 4: {id: 230 Title: "Women's Wear 3", price: 360} / / 5: {id: 40, title: "Children's Wear 1", price: 420} / /} console.log (data.list) / / Ibid}

Deep copy of output no matter which a / b value changes, the final result will not change with the change of a / b

/ / Deep copy 2 (simple and rough)

Filter filtering

Filter to print if the price is less than 300

Some

What is defined is to find a value and return

Every

This returns each condition if one of them is false and all returns false.

This is the output information.

Reduce is used to achieve cumulative effect.

The sum of the output is sum+val (numeric)

/ / reduce is used to achieve the cumulative effect (often used to write the accumulation of shopping cart prices) / / declare an array of numbers so that the numbers in it are displayed as the cumulative sum / / let arr= [200200100] / / let result = arr.reduce ((sum,val) Index) = > {/ / 200 index// 400 million 100 index// sum is the total addition and val is the value in the variable index is the index value / / console.log (sum,val,index) / / return sum + val / /}) / / console.log (result)

These are all the contents of the article "what are the methods of es6 traversing arrays". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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