In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use the Map () method in JavaScript, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Map ()
The map () method is used to apply a function to each element in the array and then return a new array. Let's take a look at grammar:
Let newArray = array.map ((v, I, a) = > {/ / return element to newArray}); / / newArray-the new array that is returned// array-the array to run the map function on// v-the current value being processed// I-the current index of the value being processed// a-the original array
The map () method can be thought of as a for loop, which is dedicated to converting values. Let's take a look at this example:
Let nums = [11,12,13,14]; let minus10 = []; for (let I = 0; I
< nums.length; i++) { minus10[i] = nums[i] - 10; } 代码生成一个新数组,其中旧数组的每个值都减少 10。是的,它有效,但有一种更简单的方法可以实现相同的结果。 现在让我们使用该map()方法重写前面的函数。 let nums = [11, 12, 13, 14];let minus10 = nums.map(value =>Value-10); / / minus10 = [1, 2, 3, 4]
Using the arrow function, we have a nice and clean function to create the same array. Because we only use the value, we only pass it to the map () method.
In this example, we will use both the value and index parameters:
Let nums = [11,12,13,14]; let newArray = nums.map ((v, I) = > {return {value: v, index: I};}); / / newArr = [/ / {value: 11, index: 0}, / / {value: 12, index: 1}, / / {value: 13, index: 2}, / / {value: 14, index: 3} / /]
I think now you'll see that anything we return in the map () method is used to create a new array. You can use the current value, the current index, or even the entire array to determine what to return.
You can even do such a stupid thing (* laugh emoji*) below and just return a static string:
Let nums = [11,12,13,14]; let cats = nums.map (() = > 'cats'); / / cats = [`cat`, `cat`]
So far, all the examples have converted all the values in the old array. Let's look at a way to convert some values in an array.
Let nums = [11,12,13,14]; ley newArr = nums.map (v = > v% 2 = = 0? V * 2: v); / / newArr = [11, 24, 13, 28]
First, we check that the remainder of the value divided by 2 is zero. If the remainder is zero, we will return v * 2 or twice the current value.
Thank you for reading this article carefully. I hope the article "how to use Map () method in JavaScript" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.