Front-end JS work common essay __js

Source: Internet
Author: User
includes (string and array conversion, string date interception, array to weight, array ordering, timestamp and time format conversion) DirectoryString and array are converted to each other the string intercepts the array, and the array sorting timestamp and the time format are converted to each other. strings and arrays are converted to each other
The conversion array let 
str = ' abcabc ' 
str.split (') Output => [' A ', ' B ', ' C ', ' A ', ' B ', ' A ', ' a ', ' C ']
str.split (' B ') output => [' A ', ' CA ', ' C ']

array spin string let
arr = [' AB ', ' CD '];
Arr.join (); Output => ' AB,CD '    
string Interception
Let str = ' 1234567890 '
str.slice (5) Output => ' 67890 '
str.slice (1,5) output => ' 2345 ' str.substring
(5) output = > ' 67890 '
str.substring (1,5) output => ' 2345 '
Array to weight
Let arr = [1,2,3,2,4,2,3,5];
Let arr=[1,1,1,2];
ES6 is as follows:
arr = [... new Set (arr)]; output =>[1,2];
Other method
function unique (arr) {
var n = [];//temporary array for
(var i = 0;i < Array.Length; i++) {
    if N.indexof ( Array[i] = = = = 1) n.push (array[i])
;
return n;
};
arr = unique (arr); Output => [1,2]
Array Sorting
Using the sort () method, the sort () method sorts the elements of the array in the appropriate place and returns the arrays. Sort sorting is not necessarily stable. The default sort order is based on the string Unicode code point.

var fruit = [' cherries ', ' apples ', ' bananas '];
Fruit.sort (); 
[' Apples ', ' bananas ', ' cherries ']

var scores = [1, n, 2]; 
Scores.sort (); 
[1, 2,]
//Note 10 before 2,
//Because in the Unicode pointer order "10" before "2"

var things = [' word ', ' word ', ' 1 word ', ' 2 Words '];
Things.sort (); 
[' 1 word ', ' 2 Words ', ' word ', ' word ']
///In Unicode, numbers before uppercase,
//uppercase before lowercase letters. 
The conversion of time stamp and time format
var time = new Date ();
Create a date instance to work with dates and times. The Date object is based on the number of milliseconds since January 1, 1970 (World Standard Time).
getTime () =>gettime () returns the number of milliseconds since January 1, 1970.
var uni_time = new Date (). GetTime ();
Output => 1511854652368

var date = new Date (1511854652368);
Y = date.getfullyear () + '-';
M = (Date.getmonth () +1 < 10? ' 0 ' + (date.getmonth () +1): Date.getmonth () +1) + '-';
D = date.getdate () + ';
h = date.gethours () + ': ';
m = date.getminutes () + ': ';
s = date.getseconds (); 
Console.log (y+m+d+h+m+s); 
Output => 2017-11-28 15:37:32;

Get the current time directly:
Console.log (New Date ()). toLocaleDateString ());
Output =>2017/11/28;

Console.log (New Date ()). toLocaleString ())
output => 2017/11/28 3:46:20
string Interception
var str = ' lywqs.com ';

Str.slice (3) and str.substring (3) 
output => ' qs.com ' (intercepted after 3rd character)

str.substring (2,5) and str.slice (2,5);
Output => ' Wqs ' (starting from the 2nd character, to the 5th character)

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.