JS数组的操作方法

2018-01-18  本文已影响17人  饥人谷_Tom

数组的长度

Array.length
var arr = [1,2,3];
console.log(arr.length); //3

数组的操作方法

插入和删除

Array.push(); 从数组的尾部插入

Array.pop(); 从数组的尾部删除,返回值是删除的元素

例子

var arr = [1,2];
console.log(arr.push(5)); //3
console.log(arr); //[1,2,5]
console.log(arr.pop()); //5
console.log(arr); //[1,2]
Array.unshift()    数组第一位插入,返回数组长度

Array.shift()   数组第一位删除,返回值是删除的值

例子

var arr = [1,2];
console.log(arr.unshift(5)); //3
console.log(arr); //[5, 1, 2]
console.log(arr.shift()); //5
console.log(arr); //[1,2]
Array.splice() 一次性解决数组添加/删除(这两种方法一结合就可以达到替换效果)

splice()方法有三个参数

  1. 开始索引(在数组中的对应索引)
  2. 删除元素的位移,即长度,如果是插入和替换则为0
  3. 插入的新元素,可以多个,也可以不写

splice()方法返回一个由删除元素组成的新数组,没有则返回空数组

var arr = [1,2,5,6,8,10];
console.log(arr.splice(1,3)); //执行删除操作,[2,5,6]
console.log(arr); //[1,8,10]
var arr = [1,2,5,6,8,10];
console.log(arr.splice(0,0,2,5,6)); //执行插入,返回值为[]
console.log(arr); //[1,2,5,6,8,10]
var arr = [1,2,5,6,8,10];
console.log(arr.splice(1,1,4));//执行替换,返回值为[2]
console.log(arr); //[1, 4, 5, 6, 8, 10]

数组的其他方法

var arr = [1,2,3,4,5,6,7,8];
console.log(arr.join("|")); //"1|2|3|4|5|6|7|8"
var arr = [1,2,3,4,5,6,7,8];

console.log(arr.slice(2)); //[3, 4, 5, 6, 7, 8]
console.log(arr.slice(1,3)); //[2, 3]
console.log(arr.slice(3,3)); //[]
console.log(arr.slice(4,3)); //[]
console.log(arr.slice(4,-3)); //[5]
console.log(arr); //[1, 2, 3, 4, 5, 6, 7, 8]
var arr1 = [1,2,3,4,5,6,7,8];
var arr2 = [100,200,300];
console.log(arr1.concat(arr2)); //[1, 2, 3, 4, 5, 6, 7, 8, 100, 200, 300]
console.log(arr2.concat(arr1)); //[100, 200, 300, 1, 2, 3, 4, 5, 6, 7, 8]
var arr = [1,2,3,4,5,6,7,8];
console.log(arr.reverse()); //[8, 7, 6, 5, 4, 3, 2, 1]
console.log(arr); //[8, 7, 6, 5, 4, 3, 2, 1]
var arr = [1,5,3,4,8,9,6,10];
console.log(arr.sort(function(v1,v2){
  return v1-v2;
})); //[1, 3, 4, 5, 6, 8, 9, 10]
console.log(arr); //[1, 3, 4, 5, 6, 8, 9, 10]

console.log(arr.sort()); //[1, 10, 3, 4, 5, 6, 8, 9]
console.log(arr); //[1, 10, 3, 4, 5, 6, 8, 9]

ES5数组拓展

var a = [];
var b = {};
console.log(Array.isArray(a)); //true
console.log(Array.isArray(b)); //false
var arr = [1,8,5,4,1,2,3,4];
console.log(arr.indexOf(2)); //5
console.log(arr.indexOf(6)); //-1
var arr = [1,8,5,4,1,2,3,4];
arr.forEach(function(e,index){
  console.log(e);
});
//1,8,5,4,1,2,3,4
var arr = [1,8,5,4,1,2,3,4];
console.log(arr.every(function(e,index){
  return e > 5;
})); //false
console.log(arr.some(function(e,index){
  return e > 5;
})); //true
var arr = [1,8,5,4,1,2,3,4];
console.log(arr.map(function(e){
  return e;
})); //[1, 8, 5, 4, 1, 2, 3, 4]
console.log(arr.map(function(e){
  return e*2;
})); //[2, 16, 10, 8, 2, 4, 6, 8]
console.log(arr); //[1, 8, 5, 4, 1, 2, 3, 4]
var arr = [1,2,3,4,-5,-6,-7,-10];
console.log(arr.filter(function(e){
  return e>0;
})); //[1, 2, 3, 4]
console.log(arr); //[1,2,3,4,-5,-6,-7,-10]
var arr = [1,2,3,4,-5,-6,-7,-10];
console.log(arr.reduce(function(v1,v2){
  return v1+v2;
})); //-18
console.log(arr.reduce(function(v1,v2){
  return v1*v2;
})); //50400
console.log(arr.reduce(function(v1,v2){
  return v1*v2;
},0)); //0
上一篇下一篇

猜你喜欢

热点阅读