Js Review (6)

On the js object, array and other types of common methods such as sorting

Object the Object

Objects in javascript can be said to account for half of the country, there is a saying called: “everything is object”, although inaccurate, but also clearly expressed the important position of objects in Js.

Object itself does not have many methods, need to remember a few commonly used.

  • Object.assign(target,source)

Allocates the values of all enumerable attributes from one or more source objects to the target object. It will return the target object.

Note: This changes the target itselfCopy the code
  • The object.entries () method returns an array of key-value pairs for the given Object’s own enumerable properties, arranged in the same order as for… The in loop returns the same order as it iterates through the object (the difference is that for-In also enumerates the properties in the prototype chain).

  • Relevant to entries are keys() and values(), which retrieve the object’s property and value, respectively

  • Object. Is (true,1)

Next comes the object method, which you can use directly

  • HasOwnProperty () checks whether it has a specified property
  • ToString () returns a string representation
  • Freeze () freezes an object, belonging to the object’s const

The use of the more important is the aforementioned Object. The prototype. ToString. The call () way to judge the type of a variable.

Here are a few common ways to do this with objects

  • Property shorthand is often used in framework development.
  • Extension operators are also very common
  • For in traverses the properties of an object

JSON related methods

  • Json. parse converts a JSON-formatted JSON string to an object
  • Json.stringify converts an object to a JSON string

It is worth mentioning that the combination of these two methods is the most common deep-copy method, as mentioned in the previous article.

An Array of Array

Array is absolutely an important way of data storage in programming language, and it is often adopted in Js to store data in the way of array + object.

  • Create new Array(20) where 20 is the length of the Array
  • Convert to Array: array.from ()
  • To heavy new Set (arr)] […
  • Merge […arr1,…arr2] / a.concat
  • The sortingarr.sort((a,b)=>{return a-b})Where a-b is in ascending order and B-a is in descending order.

Here if the array is deeper, yes

Arr = [[0, 1], [3, 4]] arr. Sort ((a, b) = > return a [0] - b [0])Copy the code

To sort by the first value of each element

  • Reverse () reverses the array
  • Pat down arr. Flat (infinity)

I used this flat to simulate doubling the array of a repeat (but it didn’t perform well)

new Array(10).fill(arr).flat(infinity)
Copy the code
  • Filter arr.filter(item=>item>0)// Filter all items greater than 0 and return a new array
  • Be satisfied with… A. every B. every C. every D. every
  • Arrays also have keys(),values(), and entries() to retrieve their contents
  • Copy copyWithin() and fill fill()
  • Convert toString(),valueOf(), and join(‘.’) to return a use. The string of connections.
  • Stack method, when we use arrays as stacks, we can use push, pop
  • Queue methods that use arrays as queues with shift and unshift
  • Slice, in which two arguments are the start and end positions, returns a string at both positions.
  • The splice method gives you more power: two arguments are delete (start, number), followed by insert or replace elements
  • IndexOf, includes, the position of the lastIndexOf to retrieve elements, and determine whether elements exist
  • Find to match the matching element value, and findIndex to return the index

The Map and Set

Map

Also called a hash table, it stores an index key and a value

  • Increment: set ()
  • Delete: delete (), clear ()
  • Alter: set ()
  • Get (), forEach (), keys (), value (), entires (), has ()

Set

Set is deweighted

  • Add: add ()
  • Delete: delete (), clear ()
  • Modification: It seems that no modification is needed
  • Check: has (), value (), entries (), forEach ()

Special reference types

The Date time

Creates a time instance that represents a moment in time.

  • New Date() is the base to get the time, no argument is the current time,

Note: if you pass 08/22 without a year, it will default to 2001

  • GetDate gets the date in January
  • GetDay gets the day of the week
  • GetMonth gets the month

Note: +1 is the exact date for this month

  • GetFullYear Gets the year of the specified date

  • GetHours gains hours

  • GetMilliseconds gets the number of milliseconds

  • GetMinutes Gets the number of minutes

  • GetSeconds Gets the number of seconds

  • GetTime Gets the time

  • Date.parse(xx) returns the number of milliseconds from 1970.1.1 to this time

  • Date.now() retrieves the number of milliseconds from 1970 to the present

Math method

  • PI (PI)
  • Absolute value of abs ()
  • CBRT () cube root sart () square root
  • Ceil takes the whole floor up and the whole round down

Number.ParseInt Converts a string to an integer

  • Max (),min() take the maximum/minimum values of the parameters
  • Random () Generates a random number

Method of a value type

Number

  • IsNaN /isFinite () /isInteger ()
  • ParseInt () /parseFloat () String to integer decimal
  • Fixed () keeps a few decimal places

String

  • The length of the length
  • The character of the chartAt digit
  • Concat stitching
  • EndsWith, startWidth with. End/beginning
  • The include contains
  • Slice segmentation
  • The split to.. Divided into the array
  • Trim, trimEnd, trimStart remove Spaces
  • IndexOf, lastIndexOf in what number
  • PadEnd/Start to fill in
  • Repeat repeat
  • Relpace/replaceAll replacement

regular

Between // 1) yuan ^ beginning $end b word begin. arbitrary w alphanumeric underscore s Blank D Digit newline S non-blank

2) repeat

  • *Zero or more
  • +1 or more
  • ? 0/1
  • {n} n times
  • {n,} n + times
  • {n,m}n~m

3) group

  • Escape: \

  • Or: |

  • Range: [] 0 ~ 9, A ~ z