1.生成随机数:Math.random()
let i; console.log(Math.random(i));
随机数范围为(0~1)
2.数字四舍五入:Math.round()
let i =Math.round((Math.random()*100)); console.log(i);
返回值范围为(0~100)
3.字符串查询子串:indexOf()
Eg:
let i ='ryyuuiioop'; console.log(i.indexOf('r',2));
返回值为-1(从i里面找r,从下标2开始找,没找到,所以返回-1)
let i ='ryyuuiioop'; console.log(i.indexOf('o',1));
返回值为7(从i里面找o,从下标1开始找,找到了,返回值为第一个o的下标)
let iml ='ryyuuiioop'; console.log(iml.indexOf('o',9));
返回值为-1(从iml下标9开始找,找o,没找到,所以返回-1)
4.字符串分割为数组:split()
Eg:
let i ='jgkjokll123'; console.log(i.split(''));
输出['j', 'g', 'k', 'j', 'o', 'k', 'l', 'l', '1', '2', '3'] 将字符串的每个字符分割为数组
let i ='jgk,jo,k,ll123'; console.log(i.split(','));
输出['jgk', 'jo', 'k', 'll123'] 会跳过i里面的,并以,前后将字符串分割为数组
let i ='jgkjokll123'; console.log(i.split('k'));
输出['jg', 'jo', 'll123'] 会跳过i里面的k,以k前后将字符串分割为数组
5.检索数组中的元素:array.indexOf()
Eg:
let i =['g','adad','ada','hgvyuv'] console.log(i.indexOf('ada'));
输出2 表示ada为数组i里面的第三个数据,也就是i[2]
6.数组拼接成字符串:join()
let a =[1,656,4523,"ujk","8645asa"]; console.log(a.join('oo'));
输出1oo656oo4523ooujkoo8645asa 表示通过oo将数组里面的数据拼接成字符串
7. 向数组中增删元素:shift头部删除 unshift头部添加 push尾部添加 pop尾部删除
Eg:
let arr1 = [1, 2, 3]; console.log(arr1.push(5)); console.log(arr1);
输出[1, 2, 3, 5]
let arr2 = [1, 2, 3]; console.log(arr2.pop()); console.log(arr2);
输出[1, 2]
let arr3 = [1, 2, 3]; console.log(arr3.unshift(5,6)); console.log(arr3);
输出[5, 6, 1, 2, 3]
let arr4 = [1, 2, 3]; console.log(arr4.shift()); console.log(arr4);
输出[2, 3]