創(chuàng)建數(shù)組
- 用逗號隔開
var array = new Array()
var array =[1,2,3,true,'yym']
arr.length
- 數(shù)組長度
var students = [
{id:1,score:80},
{id:2,score:70},
{id:3,score:90}
]
students.length //3
students = []
students.length //0
獲取數(shù)組元素
var students = [
{id:1,score:80},
{id:2,score:70},
{id:3,score:90}
]
students[0] //{id: 1, score: 80}
students[1].score = 60 //60 修改
arr.indexOf(searchElement)
- 尋找元素,獲得索引位置
var tel = [110,114,120]
tel.indexOf(110) //0 索引位置
tel.indexOf(119) //-1 沒有返回-1
arr.forEach(callback)遍歷
-
.forEach(element, index, array)- 遍歷數(shù)組,參數(shù)為一個回調(diào)函數(shù),回調(diào)函數(shù)有三個參數(shù):
當(dāng)前元素
當(dāng)前元素索引值
整個數(shù)組
var students = [
{id:1,score:80},
{id:2,score:50},
{id:3,score:70}
];
var editScore = function(item,index,array){
item.score += 5;
};
students.forEach(editScore);
var scores = [60,70,80,90]
var newScores = []
var editScores = function(item,index,array){
newScores.push(item+5)
}
scores.forEach(editScores)
console.log(newScores)
arr.reverse()
- 將數(shù)組逆序,與之前不同的是它會修改原數(shù)組
var students = [
{id:1,score:80},
{id:2,score:50},
{id:3,score:70}
];
students.reverse()
[{id: 3, score: 70}
{id: 2, score: 50}
{id: 1, score: 80}]
arr.sort([comparaFunction])
- 傳入一個回調(diào)函數(shù)
- 改變原有數(shù)組
var students = [
{id:1,score:80},
{id:2,score:50},
{id:3,score:70}
];
var byScore = function(a,b){
return b.score - a.score
}
students.sort(byScore)
// {id: 1, score: 80}
// {id: 3, score: 70}
// {id: 2, score: 50}
arr.push(element1,element2...)
- 返回的是數(shù)組長度
- 往后添加數(shù)組
var students = [
{id:1,score:80},
{id:2,score:50},
{id:3,score:70}
];
students.push({id:4,score:90}) //4
var a = new Array(1,2,3);
a.push(4);
console.log(a);//[1, 2, 3, 4]
arr.unshift(element1,element2...)
- 往數(shù)組前添加
var a=new Array(1,2,3);
a.unshift(4);
console.log(a);//[4, 1, 2, 3]
arr.shift()
- 獲取第一個數(shù)組并刪掉
var students = [
{id:1,score:80},
{id:2,score:50},
{id:3,score:70}
];
students.shift() //{id: 1, score: 80}
var a=new Array(1,2,3);
a.shift(); //1
console.log(a); //[2, 3]
arr.pop
- 獲取最后一個,并刪掉
var students = [
{id:1,score:80},
{id:2,score:50},
{id:3,score:70}
];
students.pop() //{id: 3, score: 70}
var a = new Array(1,2,3);
a.pop(); //3
console.log(a); //[1, 2]
arr.splice(index,howmany[,elements])
- index 你要從哪里刪(開始索引)
- howmany 刪掉多少(刪除元素的位移)
- 加入多少元素(插入的新元素,當(dāng)然也可以寫多個)
-
splice方法返回一個由刪除元素組成的新數(shù)組,沒有刪除則返回空數(shù)組 - 插入往前面插
替換
刪除
添加
arr.reverse() arr.sort() arr.push() arr.unshift() arr.shift() arr.pop() arr.splice() 都改變了數(shù)組
arr.slice(begin[,end])
- 從哪個索引位置開始,到哪里結(jié)束
- 開始包含,結(jié)束不包含
- 數(shù)組未改變
var num = [1,2,3,4,5,6]
num.slice(1,4) // [2, 3, 4]
num // [1, 2, 3, 4, 5, 6]
arr.concat(value1,value2,...)
- concat方法用于拼接數(shù)組
- a.concat(b)返回一個a和b共同組成的新數(shù)組,同樣不會修改任何一個原始數(shù)組,也不會遞歸連接數(shù)組內(nèi)部數(shù)組
var a = [1,2,3,4,5];
var b = [6,7,8,9];
console.log(a.concat(b));//[1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(a); //[1, 2, 3, 4, 5]
console.log(b); //[6, 7, 8, 9]
arr.join(separator)
- 分隔符
var a = [1,2,3,4,5];
console.log(a.join(',')); //1,2,3,4,5
console.log(a.join('-')); //1-2-3-4-5
arr.map(callback)
- 遍歷數(shù)組,回調(diào)函數(shù)返回值組成一個新數(shù)組返回,新數(shù)組索引結(jié)構(gòu)和原數(shù)組一致,原數(shù)組不變
var scores = [60,70,80,90]
var addScore = function(item,index,array){
return item+5
}
scores.map(addScore)
arr.reduce(callback,[initialValue])
- 后面一個參數(shù)是初始值
var students = [
{id:1,score:80},
{id:2,score:50},
{id:3,score:70}
];
var sum = function(previousResult,item,index,array){
return previousResult+item.score;
};
students.reduce(sum,0); // 200
增加的一些數(shù)組知識
Array.isArray(obj)
- 這是Array對象的一個靜態(tài)函數(shù),用來判斷一個對象是不是數(shù)組
var a = [];
var b = new Date();
console.log(Array.isArray(a)); //true
console.log(Array.isArray(b)); //false
arr.lastIndexOf(element)
-
lastIndexOf反向搜索。
var a = [1,2,3,3,2,1];
console.log(a.indexOf(2)); //1
console.log(a.lastIndexOf(2)); //4
.every(function(element, index, array)) / .some(function(element, index, array))
- 兩個函數(shù)類似于離散數(shù)學(xué)中的邏輯判定,回調(diào)函數(shù)返回一個布爾值
- every是所有函數(shù)的每個回調(diào)函數(shù)都返回true的時候才會返回true,當(dāng)遇到false的時候終止執(zhí)行,返回false
- some函數(shù)是“存在”有一個回調(diào)函數(shù)返回true的時候終止執(zhí)行并返回true,否則返回false
var a=new Array(1,2,3,4,5,6);
console.log(a.every(function(e, i, arr){
return e < 5;
})); //false
console.log(a.some(function(e,i,arr){
return e > 4;
})); //true
.filter(function(element))(類似于過濾)
- 返回數(shù)組的一個子集,回調(diào)函數(shù)用于邏輯判斷是否返回,返回true則把當(dāng)前元素加入到返回數(shù)組中,false則不加
- 新數(shù)組只包含返回true的值,索引缺失的不包括,原數(shù)組保持不變
var a = new Array(1,2,3,4,5,6);
console.log(a.filter(function(e){
return e % 2 == 0;
})); // [2, 4, 6]
console.log(a); //[1, 2, 3, 4, 5, 6]