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

Example Analysis of Array of Underscore

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you the "sample Analysis of Array of Underscore", which is easy to understand and well organized. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "sample Analysis of Array of Underscore".

Underscore provides many utility methods for Array to manipulate Array more easily and quickly.

First / last

As the name implies, these two functions take the first and last element, respectively:

'use strict';var arr = [2, 4, 6, 8]; _ .first (arr); / / 2_.last (arr); / / 8

Flatten

Flatten () takes an Array, and no matter how many Array,flatten () are nested in the Array, it turns them into an one-dimensional array:

'use strict';_.flatten ([1, [2], [3, [[4], [5]); / / [1, 2, 3, 4, 5]

Zip / unzip

Zip () aligns all the elements of two or more arrays by index and then merges them into a new array by index. For example, you have one Array that saves the name and another Array that saves the score. Now, to match the name with the score, easily implement it with zip ():

'use strict';var names = [' Adam', 'Lisa',' Bart']; var scores = [85, 92, 59]; _ .zip (names, scores); / / [['Adam', 85], [' Lisa', 92], ['Bart', 59]]

Unzip () is the reverse:

'use strict';var namesAndScores = [[' Adam', 85], ['Lisa', 92], [' Bart', 59]]; _ .unzip (namesAndScores); / / [['Adam',' Lisa', 'Bart'], [85,92,59]]

Object

Sometimes you think, instead of using zip (), why not correspond the name and score directly to Object? Don't worry, that's what the object () function is for:

'use strict';var names = [' Adam', 'Lisa',' Bart']; var scores = [85, 92, 59]; _ .object (names, scores); / / {Adam: 85, Lisa: 92, Bart: 59}

Note that _ .object () is a function, not an Object object for JavaScript.

Range

Range () allows you to generate a sequence quickly, eliminating the need for a for loop:

'use strict';// is less than 10:_.range (10) from 0; / / [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] / / less than 11:_.range (1, 11) from 1; / [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] / / less than 30 from 0, step size 5:_.range (0, 30, 5) / / [0, 5, 10, 15, 20, 25] / / greater than-10 from 0, step size-1:_.range (0,-10,-1); / / [0,-1,-2,-3,-4,-5,-6,-7,-8,-9] these are all the contents of the article "sample Analysis of Underscore Array". 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: 279

*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