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 the es6 array modifies each element

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

Share

Shulou(Shulou.com)05/31 Report--

Most people don't understand the knowledge points of this article "how to modify each element of es6 array", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "how to modify each element of es6 array".

Two methods: 1. Use forEach() to pass each element into the callback function for modification, syntax "arr.forEach(function f(v){//modify v and output}". 2. Use map() to pass each element into the callback function for modification, and finally output the processed array.

Operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

In es6, you can modify each element of an array in two ways

forEach(): Each element of the array executes a callback function once.

map(): Process each element of an array by specifying a function and return the processed array.

1. Use forEach() function

The forEach() method calls each element of the array and passes the element to the callback function.

Example: Add 1 to each element of an array

var arr = [4, 9, 16, 25];console.log("original array: "+arr);arr.forEach(function myFunction(item) { console.log("new array: "+(item+1));});

2. Use map() function

The map() method returns a new array whose elements are the processed values of the original array elements.

Example: Add 3 to each element of an array

var arr = [4, 9, 16, 25];console.log("original array: "+arr);var a1=arr.map(function myFunction(item) { return item+3;});console.log("new array: "+a1);

The above is the content of this article about "how to modify each element of es6 array". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will help everyone. If you want to know more relevant knowledge, please pay attention to 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