js常用内置对象

时间:2021-09-08 20:57:37

数组Array

1创建方式

var colors = ['red','color','yellow'];
 var colors2 = new Array();

2数组的赋值

var arr = [];
//通过下标进行一一赋值
arr[0] = 123;
arr[1] = '哈哈哈';
arr[2] = '嘿嘿嘿'

3数组的常用方法

js常用内置对象

3.1 数组的合并 concat()

    var north = ['北京','山东','天津'];
var south = ['东莞','深圳','上海'];
var newCity=north.concat(south);
console.log(newCity); //["北京", "山东", "天津", "东莞", "深圳", "上海"]

3.2 join() 将数组中的元素使用指定的字符串连接起来,它会形成一个新的字符串

var score = [98,78,76,100,0];
var str = score.join('|');
console.log(str);//98|78|76|100|0

3.3 将数组转换成字符串 toString()

var score = [98,78,76,100,0];
//toString() 直接转换为字符串 每个元素之间使用逗号隔开 var str = score.toString();
console.log(str);//98,78,76,100,0

3.4 slice(start,end); 返回数组的一段,左闭右开

var arr = ['张三','李四','王文','赵六'];
var newArr = arr.slice(1,3);
console.log(newArr);//["李四", "王文"]

3.5 pop 移除数组的最后一个元素

var arr = ['张三','李四','王文','赵六'];
var newArr = arr.pop();
console.log(newArr);//["张三", "李四","王文"]

3.6 push() 向数组最后添加一个元素

var arr = ['张三','李四','王文','赵六'];
var newArr = arr.push('小马哥');
console.log(newArr);//["张三", "李四","王文","赵六","小马哥"]

3.7 reverse() 翻转数组

var names = ['alex','xiaoma','tanhuang','angle'];

//4.反转数组
names.reverse();
console.log(names); //["angle", "tanhuang", "xiaoma", "alex"]

3.8 sort对数组排序

var names = ['alex','xiaoma','tanhuang','abngel'];
names.sort();
console.log(names);// ["alex", "angle", "tanhuang", "xiaoma"]

3.9 判断是否为数组:isArray()

布尔类型值 = Array.isArray(被检测的值) ;
var names = ['alex','xiaoma','tanhuang','angle'];
a=Array.isArray(names);
console.log(a);//True

字符串String

js常用内置对象

待续。。。。。。。。。。。。