The underscore learning array

Source: Internet
Author: User
Tags compact zip
Array Functions

1._first ([], N), _last ([], N), _initial ([], N)

_.first ([], N): takes the first n elements into an array to return, no N, and defaults back to the initial option (non-array)

_.first ([' A ', ' B ', ' C '])//n, take the first element
	"a"
_.first ([' A ', ' B ', ' C '],1)//plus n, take an array of the first n elements
	["a"]
_.first ([' A ', ' B ', ' C '],2)
	["A", "B"]
_.last ([], N): After taking an array of n elements to return, no N, the default is to return the last option (non-array);

_initial ([], N): Takes out an array that does not contain the last item;

2._.rest ([], N): Returns an array from N, N: default = 1, which does not contain the first item

_.rest ([' A ', ' B ', ' C ', ' d '],2)
	[' C ', ' d ']
_.rest ([' A ', ' B ', ' C ', ' d '])
	[' B ', ' C ', ' d ']

3._.compact ([]): Remove false value in array (null,undefined,nan,0, ')

_.compact ([0, 1, False, 2, ', 3]);
= = [1, 2, 3]
4._.flatten ([], level): Converts multiple, multidimensional arrays into an array, levels control hierarchy

All flattening
_.flatten ([1, [2], [3, [[4]]]);
= = [1, 2, 3, 4];
Flatten only the first dimensional array
_.flatten ([1, [2], [3, [[4]]]], true);
= = [1, 2, 3, [[4]]];
5. _. Union ([],[],[],...): Set

_.intersection ([],[],[],...) : Intersection

_.diffrence ([],[],[],...) : Difference Set

_.without ([],arg1,arg2): Difference set exclusion

_.uniq ([]): Array de-weight

6._.zip ([],[],[]), _unzip ([],[],[])

_.zip ([' Moe ', ' Larry ', ' Curly '], [+, +], [True, False, false]);
= = [["Moe", +, True], ["Larry", +, false], ["Curly", "false]]
_.unzip ([["Moe", +, True], ["Larry", "Max," false], ["Curly", []]);
= = [' Moe ', ' Larry ', ' Curly '], [[+, +], [True, False, false]]

7._.object ([],[]) array variable object JSON

_.object ([' Moe ', ' Larry ', ' Curly '], [+, +]);
= = {moe:30, larry:40, curly:50} _.object ([' Moe ', ' +], ['
Larry ', ' +], [' Curly ', ']]);
= = {moe:30, larry:40, curly:50}

8._.range (start:0,stop,step); Creates an array of step for start to stop

_.range (ten);
= = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
_.range (1, one);
= = [1, 2, 3, 4, 5, 6, 7, 8, 9, ten]
_.range (0,, 5);
= = [0, 5, ten, +,
0, -10,-1] _.range;
= = [0,-1,-2, -3, -4, -5,-6,-7,-8,-9]
_.range (0);
= []



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.