字符串
// split() 方法用于把一个字符串分割成字符串数组。
let str="1,1,1,1"
str.split(",") // [1,1,1,1]
数组
includes()
// 作用:包含
// 使用:
let arr = [0]
arr.includes(0) // true
arr.includes(1) // false
// 常用于:
concat()
// 作用:数组拼接
// 使用: this.server = this.server.concat(res.body.rows)
// 常用于:
push()
// 作用: 数组后面添加
// 使用: arr.push(x)
unshift()
// 数组前面插入
// 使用: arr.unshift(x)
join()
// 数组拼接成字符串
// 使用arr.join() // "1,1,1,1" 默认以逗号分隔
splice()
// 数组删除 (改变长度)
var arr = ["a","d","c","a"];
arr.splice(1,1); //输出结果:["a","c","a"]
delete()
// 数组删除 (不改变长度)
delete array[1];
对象
正则
test:
作用: 验证
使用: 验证是否为正整数
var reg = /^[1-9]\d*$/;
console.log(reg.test(msg));
//reg.test(msg) 为布尔类型
canvas
json
JSON.parse()
作用: json字符串转对象
使用: JSON.parse("[{name:'佐森',id:1}]") // [{name:'佐森',id:1}]
JSON.stringify()
作用: 对象转josn字符串
使用: JSON.parse([{name:'佐森',id:1}]) // "[{name:'佐森',id:1}]"
类型转换
字符串:
字符串转数字
parseInt(x)
parseFloat(x)
let str = "1234"
let string = parseInt(str)
字符串截取
var str = 'abcdefghij';
console.log('(1, 2): ' + str.substring(1, 2)); // '(1, 2): b'
console.log('(1, 1): ' + str.substring(1, 1)); // '(1, 1): '
console.log('(-3, 2): ' + str.substring(-3, 2)); // '(-3, 2): ab'
console.log('(-3): ' + str.substring(-3)); // '(-3): abcdefghij'
console.log('(1): ' + str.substring(1)); // '(1): bcdefghij'
console.log('(-20, 2): ' + str.substring(-20, 2)); // '(-20, 2): ab'
console.log('(2, 20): ' + str.substring(2, 20)); // '(2, 20): cdefghij'
console.log('(20, 2): ' + str.substring(20, 2)); // '(20, 2): cdefghij'
字符串替换
// 替换一条
var str = 'aaAAbBcCdDeEfghij';
var string = str.replace('a', 'A'); // string:"AaAAbBcCdDeEfghij"
// 替换所有
var str = 'aaAAbBcCdDeEfghij';
var string= str.replace(/a/g, 'A'); // string:"AAAAbBcCdDeEfghij"
json转数组对象
JSON.parse()
例:
let isJson = ""
let arr = JSON.parse(jsJson)
Math
Math.ceil()
作用: 向上取整数
使用: Math.ceil(5.2) //6
Math.floor()
作用:向下取整
使用: Math.floor(5.2) //5
Math.round()
作用: 四舍五入
使用; Math.round(1.5) // 2
Math.abs()
作用: 绝对值
使用Math.abs(-5) //5
Math.random()
作用: 返回一个0到1之间的随机数