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

Examples of the use of arrays in js

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

Share

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

This article is about examples of the use of arrays in js. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Simultaneous implementation of map and filter by Array 1 reduce method

Suppose there is a sequence now, and you want to update each item (the function of map) and filter out a portion (the function of filter). If you use map first and then filter, you need to iterate through the array twice.

In the following code, we double the values in the sequence and pick out the numbers that are greater than 50.

Const numbers = [10,20,30,40]; const doubledOver50 = numbers.reduce ((finalList, num) = > {num = num * 2; if (num > 50) {finalList.push (num);} return finalList;}, []); doubledOver50; / / [60,80] 2 counts the number of the same items in the array

In many cases, you want to count the number of repeated items in the array and represent them as an object. Then you can use the reduce method to process the array.

The following code will count the number of each type of car and then represent the total with an object.

Var cars = ['BMW','Benz',' Benz', 'Tesla',' BMW',' Toyota']; var carsObj = cars.reduce (function (obj, name) {obj [name] = obj [name]? + + obj [name]: 1; return obj;}, {}); carsObj; / / = > {BMW: 2, Benz: 2, Tesla: 1, Toyota: 1} 3 uses deconstruction to exchange parameter values

Sometimes you put multiple values returned by a function in an array. We can use array deconstruction to get each of these values.

Let param1 = 1 param2 param2 = 2; [param1, param2] = [param2, param1]; console.log (param1) / / 2console.log (param2) / / 1

Of course, we have a lot of other ways to exchange values:

Var temp = a; a = b; b = temp b = [a, a = b] [0] a = a + b; b = a-b; a = a-b4 receive multiple results returned by the function

In the following code, we get a post from / post and then get the relevant comments in / comments. Since we are using async/await, the function puts the return value in an array. After we deconstruct the array, we can assign the return value directly to the corresponding variable.

Async function getFullPost () {return await Promise.all ([fetch ('/ post'), fetch ('/ comments')]);} const [post, comments] = getFullPost (); 5 tiles the array to the specified depth

Using recursion, decrement 1 for each depth level depth. Use Array.reduce () and Array.concat () to merge elements or arrays. Basically, depth equals 1 to stop recursion. Omitting the second parameter, depth can only be tiled to a depth of 1 (single-layer tile).

Const flatten = (arr, depth = 1) > depth! = 1? Arr.reduce ((a, v) = > a.concat (Array.isArray (v)? Flatten (v, depth-1): v), []): arr.reduce ((a, v) = > a.concat (v), []); flatten ([1, [2], 3,4]); / / [1,2,3,4] flatten ([1, [2, [3, [4,5], 6], 7], 8], 2) Object deconstruction of [1, 2, 3, [4, 5], 6, 7, 8] 6 arrays

The array can also be deconstructed by objects, and the nth value of the array can be easily obtained.

Const csvFileLine = '1997 const csvFileLine Doe,US,john@doe.com,New York';const {2: country, 4: state} = csvFileLine.split (','); country / / USstate / / New Yourk Thank you for reading! This is the end of the article on "examples of the use of arrays in js". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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