数组的方法
push
- 作用:向数组
- 参数:添加具体的值,可以是一项也可以是多项
- 返回值:新数组的长度
- 是否改变原数组:改变
var ary=[2,3];
console.log(ary.push(1,5));
console.log(ary);
unshift
- 作用:在数组开头添加项,可以是一个或多个
- 参数,具体的值
- 返回值:新数组的长度
- 是否改变原数组:改变
var ary=[2,3];
console.log(ary.unshift(0,1));
console.log(ary);
pop
- 作用:删除数组末尾的一个项
- 参数:无
- 返回值:删除的最后一项
- 是否改变原数组:改变原数组长度
var ary=[2,3];
console.log(ary.pop());
console.log(ary);
shift
- 作用:删除数组第一个项
- 参数:无
- 返回值:删除的第一个项
- 是否改变原数组:改变
var ary=[2,3];
console.log(ary.shift());
console.log(ary);
splice(i,n,m)
- 作用:从i开始删除后面的n个项,并且在i后加入m项
- 参数i,n,m m可以为多个
当n为0时,就是新增
- 返回值:数组,里面包含的是删除的n项
- 是否改变原数组:改变
两个值时为在i后删除n个项
- 如果只写一个值0就是全部删除
var ary=[1,2,3];
console.log(ary.splice(1,2,9));
console.log(ary);
var ary=[1,2,3];
console.log(ary.splice(1,2));
console.log(ary);
var ary=[1,2,3];
console.log(ary.splice(0,0,5));
console.log(ary);
var ary=[1,2,3];
console.log(ary.splice(ary.length-1,1));
删除数组最后一项的方法
var ary=[1,2,3];
console.log(ary.pop());
console.log(ary.length--);
console.log(ary.splice(ary.length-1,1));
在数组最后增加项的方法
var ary = [1, 2, 3];
console.log(ary.push(4));
console.log(ary[ary.length]=4);
console.log(ary.splice(ary.length,0,4));
console.log(ary.splice(ary.length,1,4));
console.log(ary);
slice(n,m)
- 作用:复制索引n到索引m的项,但不包含索引m
- 返回值:复制的新数组
- 不改变原数组
如果参数为0或者空就会复制整个数组,如果为其他的单个值就是从那个索引开始全部复制
注意:克隆的新数组和原来的数组并不相等,因为是不同的堆内存地址;
var ary = [1, 2, 3, 4, 5, 6];
console.log(ary.slice(0));
console.log(ary.slice());
console.log(ary.slice(2, 5));
concat
- 作用:拼接数组
- 返回值:拼接好的数组
- 参数:可以为数组也可以为项
- 不改变原数组
var ary = [1, 2, 3];
var ary2=[9,10];
console.log(ary.concat(ary2,5,6));
console.log(ary);
toString
- 作用:变为字符串
- 不改变原数组
- 返回值:变为字符串的数组
- 参数:无
var ary = [1, 2, 3];
console.log(ary.toString());
console.log(ary);
join
- 作用:把数组连接符转为想要的
- 参数:"连接符"
- 返回值:字符串包裹的改变连接符后的数组
- 不改变原数组
var ary = [1, 2, 3];
console.log(ary.join('+'));
console.log(ary.join('a'));
console.log(ary);
reverse
- 作用:倒序排列数组
- 参数:无
- 返回值:倒序排列的数组
- 改变原数组,为倒序的数组
var ary = [1, 2, 3];
console.log(ary.reverse());
console.log(ary);
sort
- 作用 :排序
- 参数:
- 没有参数时只能排10以内的,且根据第一个值来排序,然后在根据第二个排序
+如果10~19可以正常拍
可以传参数为函数function(a,b){return a-b;}
- 当return a-b时为升序
- return b-a为降序
- 返回值:排序后的数组
- 改变原数组
var ary = [8, 5, 11, 22, 3, 1, 2];
console.log(ary.sort());
console.log(ary);
var ary = [8, 5, 11, 3, 1, 2];
console.log(ary.sort(function(a,b){return a-b;}));
console.log(ary);
console.log(ary.sort(function(a,b){return b-a;}));
console.log(ary);
indexOf
- 作用:检测当前值在数组中第一次出现的索引
- 参数:
- 一个值时,时当前值在数组中第一次出现的索引
两个值时,第一个值时第一次出现的索引,第二个是从哪个索引开始查找
- 返回值:当前值第一次出现的索引值
- 是否改变原数组:不改变
注意:当值不存在时返回值为-1
var ary=[1,2,1,3];
console.log(ary.indexOf(1));
console.log(ary.indexOf(1,1));
console.log(ary.indexOf(8));
lastIndexOf
- 作用:检测当前值在数组中最后一次出现的索引
- 参数:
- 一个值时,时当前值在数组中第一次出现的索引
两个值时,第一个值时第一次出现的索引,第二个是到哪个索引结束,包含第二个索引值
- 返回值:当前值第一次出现的索引值
- 是否改变原数组:不改变
注意:当值不存在时返回值为-1
var ary=[1,2,1,3];
console.log(ary.lastIndexOf(1));
console.log(ary.lastIndexOf(1,1));
console.log(ary.lastIndexOf(1,2));
console.log(ary.lastIndexOf(8));
includes
- 作用:检测值是否在数组中
- 返回值:在就是true 不在就是false
- 参数:检测的值
- 是否改变原数组:不改变
var ary=[1,2,3];
console.log(ary.includes(1));
console.log(ary.includes(8));
forEach
- 作用:遍历数组里的每一项
- 返回值:undefined
- 参数:函数function(item,index){};
- 是否改变原数组:不改变
var ary=[1,2,3,4];
console.log(ary.forEach(function(item,index){console.log(item,index); return item,index;}))
map
- 作用:映射数组
- 参数:函数function(item,index){return item;};
- 是否改变原数组:不改变
- 返回值:映射的新数组
var ary=[1,2,3,4];
console.log(ary.map(function(item,index){return item;}));
filter
- 依次便历,参数为回调函数,返回一个新数组,数组里是过滤好的数组
var res=[100, 200, 300].filter((item, index) => {
return item > 200
})
some
- 依次便历,参数为回调函数,找到符合规则的那一项,就返回true,然后就停止查询了,如果全部都没找到,就返回false
[100, 200, 300,400].some((item, index) => {
return item > 200
})
every
- 依次便历,参数为回调函数,只要数组中有一个不满足,就停止查找,返回false,如果全部满足,就返回true
[100, 200, 300,400].every((item, index) => {
return item > 200
})
find
- 发现的意思,依次便历,参数为回调函数,找到符合规则的第一项,然后return出来,找到就停止便利,没找到就是undefined
[100, 200, 300,400].find((item, index) => {
return item > 200
})
findIndex
- 发现的意思,依次便历,参数为回调函数,找到符合规则的第一项,然后return出来它的索引,找到就停止便利,没找到就是undefined
[100, 200, 300,400].findIndex((item, index) => {
return item > 200
})
自行封装了一些
Array.prototype.myFilter = function myFilter(fn) {
let obj = [];
let res;
if(typeof fn!=="function"&&fn==null)return;
for (var i = 0; i < this.length; i++) {
res = fn(this[i], i);
if (res) {
obj.push(this[i]);
}
}
return obj;
}
let a = [1, 200, 300, 2];
let res = a.myFilter((item) => {
return item > 1
})
console.log(res);
Array.prototype.mySome = function mySome(fn) {
let res;
if(typeof fn!=="function"&&fn==null)return;
for (var i = 0; i < this.length; i++) {
res = fn(this[i], i);
if(res)return true;
}
}
let a = [1, 200, 300, 2];
let res = a.mySome((item) => {
return item > 1
})
console.log(res);
Array.prototype.myEvery = function myEvery(fn) {
let res;
if(typeof fn!=="function"&&fn==null)return;
for (var i = 0; i < this.length; i++) {
res = fn(this[i], i);
if(!res)return false;
}
return res;
}
let a = [1, 200, 300, 2];
let res = a.myEvery((item) => {
return item > 200
})
console.log(res);
Array.prototype.myFind = function myFind(fn) {
let res;
if(typeof fn!=="function"&&fn==null)return;
for (var i = 0; i < this.length; i++) {
res = fn(this[i], i);
if(res)return this[i];
}
}
let a = [1, 200, 300, 2];
let res = a.myFind((item) => {
return item > 1
})
console.log(res);
数组去重
function unique(ary){
for(var i=0;i<ary.length-1;i++){
for(var j=i+1;j<ary.length;j++){
if(ary[i]==ary[j]){
ary.splice(j,1);
j--;
}
}
}
return ary;
}
var ary=[1,1,1,2,3,2,4];
var res=unique(ary);
console.log(res);
console.log(ary);
function unique(ary) {
var obj = {};
for (var i = 0; i < ary.length; i++) {
if (obj[ary[i]] == ary[i]) {
ary.splice(i, 1);
i--;
continue;
}
obj[ary[i]] = ary[i];
}
return ary;
}
var ary = [1, 1, 1, 2, 3, 2, 4];
var res = unique(ary);
console.log(res);
function unique(ary){
var newAry=[];
for(var i=0;i<ary.length;i++){
if(newAry.indexOf(ary[i])==-1){
newAry.push(ary[i]);
}
}
return newAry;
}
var ary = [1, 1, 1, 2, 3, 2, 4];
var res = unique(ary);
console.log(res);
冒泡排序
function sort(ary){
for(var i=0;i<ary.length-1;i++){
for(var j=0;j<ary.length-1-i;j++){
if(ary[j]>ary[j+1]){
var item1=ary[j];
ary[j]=ary[j+1];
ary[j+1]=item1;
}
}
}
return ary;
}
var ary=[8,2,1,5];
var res=sort(ary);
console.log(res);
快速排序

function ksort(ary){
if(ary.length<=1){
return ary;
}
var centerValue=ary.splice(Math.floor(ary.length/2),1)[0];
var leftAry=[],rightAry=[];
for(var i=0;i<ary.length;i++){
if(ary[i]<centerValue){
leftAry.push(ary[i]);
}else{
rightAry.push(ary[i]);
}
}
return ksort(leftAry).concat(centerValue,ksort(rightAry))
}
var ary=[8,2,1,5];
var res=ksort(ary);
console.log(res);
递归求和
function total(num){
if(num>0){
return 0;
}
return num+total(num+1);
}
total(1);