1, Anagramsofstring (with duplicates)
Use recursion. For each letter in a given string, a crossword puzzle is created for that letter. Use map () to combine letters with each part of the puzzle, and then use reduce () to combine all the puzzles into an array. In the most basic case, the string length is equal to 2 or 1.
constant agrams = str = & gt; {
If (string length)-> 2
Capitalize the first letter of each word.
Match the first character of each word with replace () and capitalize it with toUpperCase ().
constcapitalizeEveryWord = str = & gt; str.replace(/\b, 1)-& gt; three
7. Current URL
Use window.location.href to get the current URL.
constcurrentUrl = _ = & gtwindow . location . href;
//current URL()-& gt; ''
8. Curry
Use recursion. If the number of parameters (args) provided is sufficient, the transfer function f is called, otherwise, the simplified function f is returned.
constcurry=(fn,arity=fn.length...args)= >
Parameter quantity
Fn (... parameter)
:curry.bind(null,fn,arity,...args);
//curry(math . pow)(2)( 10)-& gt; 1024
//curry(Math.min,3)( 10)(50)(2)-& gt; 2
9、Deepflattenarray
Use recursion and use reduce () to get all elements that are not arrays. A plane is an array.
constdeepFlatten = arr = & gt
arr.reduce((a,v)= & gt; a.concat(Array.isArray(v)? deepFlatten(v):v),
,5])-& gt;
10, the difference between arrays
Create a collection from B, and then use Array.filter () on A, keeping only the values that are not included in B. ..
constdifference=(a,b)= >{ consts = newSet(b); Return. Filter (x
= & gt! s . has(x)); };
//Difference (,)->
17, filtering non-unique values in the array.
Use Array.filter () for arrays that contain only unique values.
constfilterNonUnique = arr = & gtarr . filter(I = & gt; arr.indexOf(i)===
arr . lastindexof(I));
//filterNonUnique()-& gt;
18, flatten the array
Use reduce () to get all the elements in the array, and use concat () to flatten them.
constflatten = arr = & gtarr.reduce((a,v)= & gt; a.concat(v),)-& gt;
19, get the maximum value from the array
Use Math.max () and spread operator (...) to get the maximum value in the array.
constarrayMax = arr = & gtMath.max(...arr);
//array max()-& gt; 10
20. Get the minimum value from the array
Use Math.min () and spread operator (...) to get the minimum value in the array.
constarrayMin = arr = & gtMath.min(...arr);
//array min()-& gt; 1
2 1, get the scroll position.
If defined, please use pageXOffset and pageYOffset, otherwise use scrollLeft and scrollTop to omit the default value of el using window.
constgetScrollPos =(El = window)= & gt;
({x:(el.pageXOffset! = = undefined)? el.pageXOffset:el.scrollLeft,
y:(el.pageYOffset! = = undefined)? El . pageyoffset:El . scroll top });
//getScrollPos()-& gt; {x:0,y:200}
22, the greatest common divisor (GCD)
Use recursion. The basic case is when y equals 0. In this case, x is returned. Otherwise, return the GCD of y and the rest of x/y.
constgcd=(x,y)= & gt; ! y? x:gcd(y,x % y);
//gcd(8,36)-& gt; four
23. Top card
Returns array [0]
consthead = arr = & gtarr[0];
//head()-& gt; 1
24, list initialization
Return arr.slice(0,-1)
constitinitial = arr = & gt; arr.slice(0,- 1);
//initial()-& gt;
The above is the article about JavaScript code fragment (1) shared by Bian Xiao today. I hope this article can be helpful to friends who are engaged in Web front-end work. For those who want to know more about the front end of the web and the code fragments behind it, remember to pay attention to the training of official website and Beida Jade Bird web.