Summary for beginners - js grammar summary

keep decimals

var a = 3.123
b=a.toFixed(1) retains n decimal places
c=parseFloat(a)
d=parseInt(a)
console.log(b)//print 3.1
console.log(c)//Print 3.123 (if the decimal part exists, it will be printed out) [But is it necessary?] [It is necessary, how to add, subtract, multiply and divide the string type! ! 
console.log(d)//print 3
The combination is as follows
e=parseFloat(a.toFixed(2))//If a is an integer, the integer will be obtained after printing, and no decimals are reserved (perhaps only js)

 

 

output after converting the object to an array

var info={a:1,b:2}
info.a=1
info.b = 2
var new = JSON.stringify(info)
console.log(new)

 

 

pop up

var answer = prompt('7%5 = ?');
if (answer == 2){
    console.log('Correct !! ');
}                                                    //prompt

 

 

return data type

【typeof()】

 

object length

var a=Object.keys(object_name) //All property names of the object are output when printing
  a.length //The length of the output object when printing

 

Object property value

var obj = {'name':'Wang Er','age':500}
console.log(Object.value(obj)) //print ['Wang Er', 500]

 

Object property name (key)

var obj = {'name':'Wang Er','age':500}
console.log(Object.keys(obj)) //Print enumerable property names  
console.log(Object.getOwnPropertyNames(obj)) //Print all property names including enumerable and non-enumerable

 

split string

[String name.split(separator,howmany)

separator is required. String or regular expression to split the string from the place specified by this parameter.

howmany is optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter is not set, the entire string will be split, regardless of its length.

The return value is an array of strings. The array is created by splitting the string stringObject into substrings at the boundaries specified by the separator. The strings in the returned array do not include the separator itself.

However, if the separator is a regular expression containing subexpressions, the returned array includes strings that match those subexpressions (but not text that matches the entire regular expression).

 

Traversing an array (forEach)

array name.forEach(function(value,index){method content}, object name)

[1. The function parameters are value and index;

2. It is to make a function call to each element in the array in turn;

3. Object name: the object pointed to by this (if there is this) in the previous function (in fact, I haven't tried it yet, not sure if this is the case);

4.value: As a formal parameter, it represents an element in the array (if it is an array of objects, it represents an object value.key);

5. index: I haven’t used this parameter yet (or used it accidentally), so I don’t know what to do.]

 

indexOf

[Array name/string.indexOf(searchvalue,fromindex)

searchvalue Required. Specifies the string value to retrieve.

fromindex Optional integer parameter. Specifies the position in the string at which to start searching. Its legal values ​​are 0 to stringObject.length - 1.

                        If this parameter is omitted, the search will start from the first character of the string.

说明:该方法将从头到尾地检索数组/字符串,看它是否含有子串 searchvalue。开始检索的位置在字符串的 fromindex 处或数组/字符串的开头(没有指定 fromindex 时从头开始)。如果找到一个 searchvalue,则返回 searchvalue 的第一次出现的位置。stringObject 中的字符位置是从 0 开始的。

注释:indexOf() 方法对大小写敏感!

注释:如果要检索的值没有出现,则该方法返回 -1。】

 

添加删除项目

【 数组名.splice (  index  ,  howmany  ,  item1 ,....., itemX )

index               必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。

howmany        必需。要删除的项目数量。如果设置为 0,则不会删除项目。

item1, ..., itemX可选。向数组添加的新项目。

 JavaScript 使用下列规则将非布尔值转换为布尔值:

1.所有对象都被视为 true。

2.当且仅当字符串为空时才被视为 false。

3.null 和未定义被认为是 false。

4.当且仅当数字为 0 时才为 false。

 

 js取整

【Math.floor   (数字或算式)向下取整

   Math.round (数字或算式)四舍五入

   Math.ceil     (数字或算式)向上取整

   a%b 取余】

 

获取时间

【var myDate = new Date();                  //获取当前详细时间

myDate.getYear();                           //获得当前年份(2位)

myDate.getFullYear();                       //获得完整的年份(4位,1970-????)

myDate.getMonth();                          //获得当前月份(0-11,0代表1月)

myDate.getDate();                           //获得当前日(1-31)

myDate.getDay();                            //获得当前星期X(0-6,0代表星期天)

myDate.getTime();                           //获得从1970.1.1开始至现在的毫秒数

myDate.getHours();                          //获得当前小时数(0-23)

myDate.getMinutes();                        //获得当前分钟数(0-59)

myDate.getSeconds();                        //获得当前秒数(0-59)

myDate.getMilliseconds();                   //获得当前毫秒数(0-999)

myDate.toLocaleDateString();                //获得当前日期

var mytime=myDate.toLocaleTimeString();     //获得当前时间的字符型

myDate.toLocaleString( );                   //输出当前日期与时间】

 

合并对象

Object.assign()
var o1 = { a: 1 };
var o2 = { b: 2 };
var o3 = { c: 3 };
var obj = Object.assign(o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
console.log(o1);  // { a: 1, b: 2, c: 3 }// 注意目标对象自身也会改变。

 

 

 

--未完待续--

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326594415&siteId=291194637
Recommended