基本数据类型
引用数据类型
数组类型也是object,是特殊的对象
let arr = [1,2,3,4];
console.log(typeof arr);
//object
1. 数组的增删改的方法
这一部分方法都会修改原有数组
push():向数组末尾增加内容
let arr = [1,2];
let newArr = arr.push(5,'a',{
name:"limy"});
console.log(newArr);
//5
console.log(arr);
//[1,2,5,'a',{name:"limy"}]
unshift():向数组开始位置增加内容
let arr = [1,2];
let newArr = arr.unshift(5,'a',{
name:"limy"});
console.log(newArr);
//5
console.log(arr);
//[5,'a',{name:"limy"},1,2]
shift():删除数组中第一项
let arr = [1,2,3];
let item = arr.shift();
console.log(item);
//1
console.log(arr);
//[2,3]
pop():删除数组中最后一项
let arr = [1,2,3];
let item = arr.pop();
console.log(item);
//3
console.log(arr);
//[1,2]
splice():能实现数组的增加、删除、修改
删除
//-------- n,m都有值,从索引n开始,删m个 ---------
let arr = [1,2,3,4,5,6,7];
let item = arr.splice(2,3);
console.log(item);
//从索引2开始删除的3个元素为:[3,4,5]
console.log(arr);
//修改后的数组为[1,2,6,7]
//-------- 不传m的值,从索引n开始,删到最后---------
let arr = [1,2,3,4,5,6,7];
let item = arr.splice(1);
console.log(item);
//从索引1开始删除至末尾的元素为:[2,3,4,5,6,7]
console.log(arr);
//修改后的数组为[1]
//-------- 删除数组最后一项 ---------
let arr = [1,2,3];
arr.splice(arr.length-1);
//-------- 删除数组第一项 ---------
let arr = [1,2,3];
arr.splice(0,1);
增加、修改
//-------- 修改 ---------
let arr = [1,2,3,4,5,6,7];
let item = arr.splice(1,3,'aa','bb');
console.log(item);
//[2,3,4]
console.log(arr);
//[1, "aa", "bb", 5, 6, 7]
//-------- 增加 ---------
//在开头增加
let arr = [1,2];
let item = arr.splice(0,0,'aa','bb');
console.log(item);
//[]
console.log(arr);
//['aa','bb',1,2]
//在末尾增加
let arr = [1,2];
let item = arr.splice(arr.length,0,'aa','bb');
console.log(item);
//[]
console.log(arr);
//[1,2,'aa','bb']
2. 数组的查询和拼接
这一部分方法不改变原数组
slice():能实现数组的查询
let arr = [1,2,3,4,5];
let item = arr.slice(1,3);
console.log(item);
//[2,3]
※ 利用slice()进行数组的克隆(浅克隆)
let arr = [1,2,3,4,5];
let cloneArr= arr.slice(0);
console.log(cloneArr);
//[1,2,3,4,5]
concat():能实现数组的拼接
let arr1 = [1,2,3,4,5];
let arr2 = [10,20,30];
let item = arr1.concat('limy',arr2);
console.log(item);
// [1, 2, 3, 4, 5, "limy", 10, 20, 30]
3. 把数组转换为字符串
这一部分方法不改变原数组
toString():把数组转为字符串
let arr = [1,'limy',{
x:3}];
let item = arr.toString();
console.log(item);
// 1,limy,[object Object]
console.log([]).toString();
//""
console.log([12]).toString();
//"12"
join():把数组转为字符串(强大版)
let arr = [1,'limy',333];
let item = arr.join();
console.log(item);
//1,limy,333 不传参默认用“,”分隔
let item = arr.join("|");
console.log(item);
//1|limy|333
//----用来求和----
let arr = [1,22,333];
let item = arr.join("+");
console.log(eval(item));
//356
4. 检测数组中是否包含某一项
indexOf / lastIndexOf :检测当前项在数组中第一次或最后一次出现位置的索引值
let arr = [1,'limy',333,'limy'];
console.log(arr.indexOf('limy'));
//1
console.log(arr.lastIndexOf('limy'));
//3
if(arr.indexOf(30) === -1)
{
//不包含
console.log('不包含30')
}
console.log(arr.includes(30))
//false(ES6新提供方法)
5. 数组的排序或排列
原来数组改变
reverse():把数组倒过来排列
let arr = [1,'limy',333,5555,'哈哈'];
arr.reverse();
console.log(arr);
// ["哈哈", 5555, 333, "limy", 1]
sort():数组排序
sort()默认按第一个字符来排序
let arr = [333,55,1];
arr.sort();
console.log(arr);
//[1, 333, 55]
传参数:函数,则可以进行数字排序
let arr = [333,55,1,44,92];
//【升序】
arr.sort((a,b) => a-b;);
console.log(arr);
//[1, 44, 55, 92, 333]
//【降序】
arr.sort((a,b) => b-a);
console.log(arr);
//[333, 92, 55, 44, 1]
6. 遍历数组中每一项的方法
forEach():遍历数组中的每一项内容
let arr = [333,55,1,44,92];
arr.forEach((item,index) =>{
console.log("索引:"+index,",内容:"+item);
})
1. 用includes
let arr = [1,2,1,3,2,4,5,2,1,4,5,2,4,1];
let newArr = [];
for(var i=0; i<arr.length; i++)
{
if(newArr.includes(arr[i])) continue;
newArr.push(arr[i]);
}
console.log(newArr)
2. 通过object
let arr = [1,2,1,3,2,4,5,2,1,4,5,2,4,1];
let obj = {
};
for(var i=0;i<arr.length;i++)
{
var item = arr[i];
if(obj[item] != undefined)
{
//这一项已存在,用最后一项覆盖它,再删除最后项
arr[i] = arr[arr.length-1];
arr.length --;
i --;
continue;
}
obj[item] = item;
}
3. 基于ES6的Set(对应的Map)实现去重
let arr = [1,2,1,3,2,4,5,2,1,4,5,2,4,1];
arr = [...new Set(arr)];