This is the 30th day of my participation in the Wenwen Challenge

Search “Program Yuan Xiaozhuang” on wechat, and you will get a full set of Python full stack tutorials, as well as the constantly updated e-books and interview materials prepared by Xiaozhuang

preface

Don’t js have data types like lists in Python? And no dictionary? The answer, of course, is drip, please see Xiao Zhuang slowly.

object

JavaScript is an object-oriented language. Everything is an object for JavaScript, and JavaScript also allows you to customize objects.

The array object

Arrays in JavaScript are similar to lists in Python, also represented by [].

Basic array methods

var a = [11.'a'.true];
typrof a; // "object"

// Index values are supported, but negative indexes are not
a[1]; // "a"


// Array length:.length
a.length;  / / 3

// Append values to the array
a.push('chloe');
a; // (4) [11, "a", true, "chloe"]

// Array delete value: pop(),n does not write, default to delete the last element, will return the value of the deleted element;
a.pop();

// unshift(): inserts elements in the header, returns the number of elements in the current array
a.unshift('false');  / / 4
a  // (4) ["false", 11, "a", true]

// Shift (): the header removes the element, returning the value of the removed element
a.shift(); // "false"

//. Slice (start,end)
a.slice(0.2); // (2) [11, "a"]

/ / reverse () : inversion
a.reverse(); // (3) [true, "a", 11]

//.join(): uses the specified string concatenation to internally convert numbers to strings
var a = [true."a".11];
a.join(2); // "true2a211"


// concat(): the python equivalent of the extend method for lists, returns an extended tuple
a.concat([1.2.3]);  // (6) [true, "a", 11, 1, 2, 3]

/ / the sort () : sorting
var a = [2.2.4.3];
a.sort();  / / (4) (2, 2, 3, 4]
Copy the code

Important method – forEach method

// forEach(function(value,index,arr){console.log(value,index,arr)})
var a = [1.2.'a'.'c'];

// There is only one value argument, which displays the values of the elements in the array
a.forEach(function(value){console.log(value)});
/*
VM1274:2 1
VM1274:2 2
VM1274:2 a
VM1274:2 c
*/

// Two parameters value index, display the array element value and element index
a.forEach(function(value,index){console.log(value,index)});

VM1326:1 1 0
VM1326:1 2 1
VM1326:1 a 2
VM1326:1 c 3


// Value index arr displays the value of the element in the array with the index and the element's source
a.forEach(function(value,index,arr){console.log(value,index,arr)});

VM1340:1 1 0 (4) [1.2."a"."c"]
VM1340:1 2 1 (4) [1.2."a"."c"]
VM1340:1 a 2 (4) [1.2."a"."c"]
VM1340:1 c 3 (4) [1.2."a"."c"]

// Value index arr xx displays the values and subscripts of the elements in the array and undefined
a.forEach(function(value,index,arr,xx){console.log(value,index,arr,xx)});

VM1372:1 1 0 (4) [1.2."a"."c"] undefined
VM1372:1 2 1 (4) [1.2."a"."c"] undefined
VM1372:1 a 2 (4) [1.2."a"."c"] undefined
VM1372:1 c 3 (4) [1.2."a"."c"] undefined
Copy the code

Important method – Splice

// splice(start,num,data) : three parameters, the first is the start position, the second is the number of deleted, and the third is the data added after deletion

var a = [1.2.'a'.'c'];

// In the case of two arguments, the first is the starting position, and the second is the number of deleted elements
a.splice(1.1);  / / [2]
a; // (3) [1, "a", "c"]

// Three parameters, delete first then add
// Add the element after deletion
a.splice(1.1.4); / / [2]
a; / / [1, 4, 'a', 'c']
// Add array after delete
a.splice(1.1[4.5]); / / [2]
a;  // (4) [1, Array(2), "a", "c"]
Copy the code

Important method – Map

// map(function(value,index,arr){return value}): Function (value,index,arr){return value})

var a = [1.2.'a'.'c'];
res = a.map(function(value){return value+1}); // (4) [2, 3, "a1", "c1"]
a; // (4) [1, 2, "a", "c"]
Copy the code

Custom object

Custom objects in JavaScript can be thought of as dictionaries in Python, but custom objects in JavaScript are much easier to manipulate than dictionaries in Python.

Creating a custom object – {}

// Create a custom object
var d = {'name':'chloe'.'age':18};

// Check the type of the object
typeof d; // "object"

/ / value
d['name']; // "chloe"
d['age'];  / / 18
d.name;  // "chloe"
d.age;  / / 18

// The for loop is supported. The default value is key
for (let i in d)
{
	console.log(i,d[i]);
};
/*
name chloe
age 18
*/
Copy the code

Create a user-defined object using method 2 -new

var d = new Object(a);/ / {}

// Add a key-value pair to an object
d['name'] = 'chloe';
// Add key-value pairs
d.age = 18;
d; // {name: "chloe", age: 18}
Copy the code

The Date object

let d = new Date(a); d;// 
Sat May 16 2020 08:38:28 GMT+0800(China Standard Time)// Convert the above time to a more convenient viewing format
d.toLocaleString(); // "2020/5/16 8:38:28 am"


// Manual time input is supported
let d = new Date('2200/11/11 11:11:11');
d; Tue Nov 11 2200 11:11:11 GMT+0800 Tue Nov 11 2200 11:11:11 GMT+0800
d.toLocaleString();  // "2200/11/11 11:11:11 am"

// Note: months are from 0 to November
// Time object concrete method
let d6 = new Date(a); D6.getdate () gets the day d6.getDay() gets the week d6.getMonth() gets the month0-11GetHours () gets hours d6.getminutes () gets minutes d6.getseconds () gets seconds d6.getmilliseconds () gets milliseconds D6. GetTime () time stampCopy the code

Highlight – JSON object

Json is an important part of any language to enable interaction between different languages.

// serialize,stringify()-- the equivalent of dumps in Python

let j = {'name':'chloe'.'age':18};
let res = JSON.stringify(j);
res;  // "{"name":"chloe","age":18}"

// Deserialize, parse() ---- corresponds to Load in Python
JSON.parse(res);  // {name: "chloe", age: 18}
Copy the code

The Math object

Abs (x) returns the absolute value of the number. E to the x returns the exponent of e. Floor (x) logarithms are rounded down. Log of x is the natural log of the number returned (base e). Max (x,y) returns the highest value of x and y. Min (x,y) returns the lowest value of x and y. Pow (x,y) returns x to the y power. The random () returns0 ~ 1Random number between. Round (x) Rounds the number to the nearest whole number. Sine of x returns the sine of a number. SQRT (x) returns the square root of the number. Tangent of x returns the tangent of the Angle.Copy the code

The operator

Arithmetic operator

JavaScript supports the normal +-×/ operation.

++ : indicates the increment1, similar to +=1-- : ++var x = 10;
// Assign x to y first,x is incrementing
var y = x++;
x; / / 11
y; / / 10

var x = 10;
// x increments first, and then the incremented x is assigned to y
var y = ++x;
x; / / 11
y; / / 11
Copy the code

Comparison operator

// == weak equals, internal automatically converted to the same data type for comparison
1= ='1'; // true

// === = strong ==, no type conversion inside
1= = ='1'; // false

/ / case
1! ='1'; // false
1! = ='1'; // true
Copy the code

Logical operator

Short circuit operation: Boolean and sum or operation. Which value is returned after the final result has been computed to a certain value.

And relationships: && or relationships: | | not relationships:!5 && '5';
'5'

0 || 1;1

!5 && '5';
false
Copy the code

conclusion

The article was first published on the wechat public account Program Yuan Xiaozhuang, and synchronized with nuggets and Zhihu.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)