让前端飞

2. JavaScript Array

2017-06-08  本文已影响0人  moonburn

此文章用于归纳Array的所有方法


在JavaScript中,除了Object,用得最多的可能就是数组Array了。下面我们将一起来总结一下数组Array的所有方法。

首先总结一下Array的属性:

然后总结一下Array的方法:

function arr(){
 return  Array.from(arguments, function(num){return num*2});   
}
arr(1,2,3,4);//[2, 4, 6, 8]
Array.of(2);       // [2] 
Array.of(1, 2, 3); // [1, 2, 3]
Array(2);          // [ , ]或者[undefined × 2]
Array(1, 2, 3);    // [1, 2, 3]

最后总结Array实例的方法,也就是array.prototype的方法:

var arr1 = ['a', 'b', 'c'];
var arr2 = ['d', 'e', 'f'];
arr1.concat(arr2);//["a", "b", "c", "d", "e", "f"]
arr1.concat(1,2,3)//["a", "b", "c", 1, 2, 3]
//从第3个位置开始替换倒数第2个到倒数第1个元素,即5
[1,2,3,4,5,6].copyWithin(3,-2,-1)//[1,2,3,5,5,6]
//从第0个位置开始替换第2到第4的元素,即3,4
[1,2,3,4,5,6].copyWithin(0,2,4)//[3,4,3,4,5,6]
var a = ['a', 'b', 'c'];
var iterator = a.entries();
console.log(iterator.next()); //Object {value: Array(2), done: false}
for (let e of iterator) {
  console.log(e);
}
// [1, 'b']
// [2, 'c']
[1,2,3,4,5,6].every(function(item,index){
   return item>5
})//false
[1,2,3,4,5,6].every(function(item,index){
   return item>0
})//true
[1,2,3,4,5,6].some(function(item,index){
   return item>5
})//true
[1,2,3,4,5,6].some(function(item,index){
   return item>9
})//false
//用3替换倒数第2个到倒数第1个元素,即5
[1,2,3,4,5,6].fill(3,-2,-1)//[1,2,3,4,3,6]
//用0替换第2到第4的元素,即3,4
[1,2,3,4,5,6].fill(0,2,4)//[1,2,0,0,5,6]
[1,2,3,4,5,6].filter(function(item,index){
   return item>5
})//[6]
[1,2,3,4,5,6].filter(function(item,index){
   return item>2
})//[3,4,5,6]
[1,2,3,4,5,6].find(function(item,index){
   return item>5
})//6
[1,2,3,4,5,6].find(function(item,index){
   return item>2
})//3
[1,2,3,4,5,6].findIndex(function(item,index){
   return item>5
})//5
[1,2,3,4,5,6].findIndex(function(item,index){
   return item>2
})//2
var arr = [];
[1,2,3,4,5,6].forEach(function(item,index){
  if(item>2){ arr.push(item); }
})
console.log(arr)//[3, 4, 5, 6]
var arr = [1,2,3]
arr.includes(2);//true
arr.includes(5);//false
arr.includes(2,2)//false
var arr = [1,2,3]
arr.indexOf(2);//1
arr.indexOf(5);//-1
arr.indexOf(2,2)//-1
var arr = [1,2,3,1]
arr.lastIndexOf(2);//3
arr.lastIndexOf(5);//-1
arr.lastIndexOf(2,2)//-1
var a = ['a', 'b', 'c'];
a.join();      // 'a,b,c'
a.join(', ');  // 'a, b, c'
a.join(' + '); // 'a + b + c'
a.join('');    // 'abc'
var a = ['a', 'b', 'c'];
var iterator = a.keys();
console.log(iterator.next()); //Object {value: 0, done: false}
for (let e of iterator) {
console.log(e);
}
//1
//2
[1,2,3,4,5,6].map(function(item,index){
return item*2
})//[2, 4, 6, 8, 10, 12]
var arr = [1, 2, 3];
arr.pop();//3
console.log(arr); // [1, 2]
var arr = [1, 2, 3];
arr.push(4);//4
console.log(arr); // [1,2,3,4]
var arr = [1,2,3,4,5,6]
arr.reduce(function(element1,element2){
    return element1+element2
})//21
var arr = [1,2,3,4,5,6]
arr.reduceRight(function(element1,element2){
    return element1+element2
})//21
var arr = [1,2,3,4,5,6]
arr.reverse()
var arr = [1, 2, 3];
arr.shift();//1
console.log(arr); // [2, 3]
var arr = [1,2,3,4,5,6]
//选择第2和第4,即3,4
arr.slice(2,4)//[3,4]
console.log(arr)// [1,2,3,4,5,6]
var arr = [1,2,3,4,5,6,7,8]
//从第2开始,删除4个元素,并添加moonburn字符串
arr.splice(2,4,'moonburn')//[3,4,5,6]
console.log(arr)// [1,2,'moonburn',7,8]
var arr = [1,20,3,40,5,60]
arr.sort(function(element1,element2){
    return element1>element2
})
console.log(arr)// [1, 3, 5, 20, 40, 60]
var arr = [123,new Date(),'string']
arr.toLocaleString()//"123,2017-6-8 11:04:50,string"
var arr = [123,new Date(),'string']
arr.toString()//"123,Thu Jun 08 2017 11:08:24 GMT+0800 (中国标准时间),string"
var arr = [1,2,3,4];
arr.unshift('a','b');//6
console.log(arr);//["a", "b", 1, 2, 3, 4]
上一篇下一篇

猜你喜欢

热点阅读