Recently, I was also preparing for the interview, in the review algorithm, witty use of a wave of ES6. Come and see!
1. The de-weight of the array
var arr=str.split ('); for (var i=0;i<arr.length-1;i++) { for (var j=i+1;j<arr.length;j++) { if(arr[i]==arr[j]) { Arr.splice (J,1); // arr.length-=1; Delete the length automatically minus 1 j--; Console.log (arr) }}}
It seems a bit troublesome to do it with two layers of loops. It seems that the set in ES6 can be used. Try?
var arr=[1,3,55,12,32,3,66,12,358,66,73,32]; var a=New Set (arr); // set is a collection of class arrays Console.log (a,a.size); // Arr=array.from (a); Converts an array of classes to array arr=[...a]; Extension Operators
What? So easy, not so much logic, and very little code. It's so cool!!!
2. How to splice two arrays?
The traditional for loop does not say much, of course concat is also possible, or directly on the ES6 bar!
var = [arr1]; var arr2 = [4,5,6]; Arr1.push (... arr2);
or something like that.
var = [arr1]; var arr2 = [4,5,6]; var arr3 = [... arr1,... arr2];
Later, you will continue to write questions about the algorithm, or ES6. Want to feel good small partners, can order a praise add a concern about what.
(When writing the first blog, the content is too much, too miscellaneous, will walk the text less, concise style, after all, I look at others blog when, content too much or miscellaneous words, it is easy to head pain).
PS: Never copy other people's blogs, pure their own hands to fight.
With ES6 clever solution to the traditional interview algorithm small problem!