数组:
数组对象是使用单独的变量名来存储一系列的值。
数组中的元素可以是任意的数据类型
创建
构造函数创建 new Array();
()中可以是数组的长度,或者包含的项;
var colors=new Array(1);
var names=[];
// 1.准备工作
var names=new Array("小张","小米","小明");
var colors=new Array(3);
// 2.读数组
console.log(names[1]);//小米
// 3.写数组数据
colors[0]="red";
colors[1]="green";
colors[2]="black";
colors[3]="blue";
colors[3]="yellow"
console.log(colors);
// 1.准备工作
var names=new Array("小张","小米","小明");
// 2.获取长度
console.log(names.length);//3
// 3.添加/删除数组元素
names.length=2;
console.log(names);//(2) ["小张", "小米"]
names[5]="小孩";
console.log(names);//(6) ["小张", "小米", empty × 3, "小孩"]
// 1.准备工作
var names=new Array("小张","小米","小明");
var colors=new Array(3);
// 2.push()方法
console.log(names);//["小张", "小米", "小明"]
var le=names.push("小黑");
console.log(names);//["小张", "小米", "小明", "小黑"]
console.log(le);//4
// 1.准备工作
var names=new Array("小张","小米","小明");
var colors=new Array(3);
// 4.pop()方法
var de=names.pop();
console.log(de);//小明
console.log(names);//(2) ["小张", "小米"]
// 1.准备工作
var names=new Array("小张","小米","小明");
var colors=new Array(3);
// 5.shift()方法
var dd=names.shift();
console.log(dd);//小张
console.log(names);//["小米", "小明"]
// 1.准备工作
var names=new Array("小张","小米","小明");
var colors=new Array(3);
// 3.unshift()方法
var un=names.unshift("小猪");
console.log(un);//4
console.log(names);//["小猪", "小张", "小米", "小明"]
// 1.准备工作
var names=new Array("小张","小米","小明");
var colors=new Array(3);
// join()
var str=names.join();
console.log(str);//小张,小米,小明
var str=names.join("-");
console.log(str);//小张-小米-小明
// 1.准备工作
var names=new Array("小张","小米","小明");
var colors=new Array(3);
// reverse()
console.log(names);//[ "小张", "小米", "小明"]
var arr=names.reverse();
console.log(arr);//["小明", "小米", "小张"]
// sort()方法
var num1=[1,3,12,9,18,55,31,-4,-46];
console.log(num1);//[1, 3, 12, 9, 18, 55, 31, -4, -46]
var num2=num1.sort();
console.log(num2);//[-4, -46, 1, 12, 18, 3, 31, 55, 9]
// 升序排列
var num3=num1.sort(function(a,b){return a-b})
console.log(num3);//[-46, -4, 1, 3, 9, 12, 18, 31, 55]
// 降序排列
var num4=num1.sort(function(a,b){return b-a})
console.log(num4);//[55, 31, 18, 12, 9, 3, 1, -4, -46]
// concat()方法
var arr1=["a","b","c"];
var arr2=["d","e","f"];
var arr3=arr1.concat(arr2);
console.log(arr1);//["a", "b", "c"]
console.log(arr3);//["a", "b", "c", "d", "e", "f"]
// slice()方法
var colors=["red","blue","yellow","black","white","green"]
// var newcolors=colors.slice(2);//"yellow","black","white","green"
// var newcolors=colors.slice(-1);//"green"
//var newcolors=colors.slice(0 ,2);//"red", "blue"
var newcolors=colors.slice(-4,4);//["yellow", "black"]
console.log(newcolors);//["yellow", "black"];
splice()方法
10.1删除功能
语法:Array.splice(index,count)
参数:index:开始索引
count:删除的数量(如果为0,则不删除;如果不设置,则删除到数组结尾);
功能:删除从index开始的几个元素;
返回值:被删除的元素
// splice()方法 删除功能
var words=["a","b","e","c","d","e","f","g"];
var del=words.splice(2,3);//["c", "d", "e"]
var del=words.splice(4,0);//[]
var del=words.splice(4);//["e", "f", "g"]
console.log(words);
console.log(del);
10.2插入功能
语法:Array.splice(index,0,item1,…,itemx)
参数:
index:插入的索引(在元素的某个位置前插,就是该元素的索引;某个元素后插,该元素索引+1);
0:被删除元素为0个;
item:插入的元素
功能:在指定位置插入数据;
返回值:数组
// splice()方法 插入功能
var words=["a","b","e","c","d","e","f","g"];
var insert=words.splice(3,0,"g","h");
console.log(words);//["a", "b", "e", "g", "h", "c", "d", "e", "f", "g"]
console.log(insert);//[]
10.3替换功能
语法:Array.splice(index,count,item1,…,itemx)
功能:在指定位置插入值,并且同时删除任意数量的项;
参数:index:起始位置
count:删除的项数
item:新增元素;
返回值:从原数组中删除是元素
// splice()方法 替换功能
var words=["a","b","e","c","d","e","f","g"];
var replace=words.splice(3,2,"k","l","m");
console.log(words);//["a", "b", "e", "k", "l", "m", "e", "f", "g"]
console.log(replace);// ["c", "d"]
// indexOf() 方法:查找指定元素的第一次出现索引
var words=["a","b","e","c","d","e","f","g"];
var val=words.indexOf("e")//2
var val=words.indexOf("e",3)//5
console.log(val);
// lastindeOf()方法: 查找指定元素最后一次出现索引值
var words=["a","b","e","c","d","e","f","g"];
var val=words.lastIndexOf("e");//5
var val=words.lastIndexOf("e",2);//5 2为查找范围的末尾值
console.log(val);