操作陣列可以先區分為兩大類:
會改變原始陣列長相
與複製一個原始陣列來更改
陣列圖片來源:[偷米騎巴哥]操作JS陣列的20種方式
.map
.filter
.slice
concat
.map(function(item, index, array) {return }
產生一個新陣列
any = ['A', 'B', 'C', 'D', 'E'];
let any2 = any.map(function(item, index, array) {
return item + item;
})
console.log(any2);
// ["AA", "BB", "CC", "DD", "EE"]
console.log(any);
// ["A", "B", "C", "D", "E"]
Map 不會對原有的陣列受到任何影響
範例三:組合人名第一個字與最後一個字
字串空格
或是用 eslint
console
.filter(function(item 當前的值, index, array) {return }
篩選.filter(function(item, index, array) {return }
item
物件值index
陣列索引array
陣列本身
any = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let newAny = any.filter(function(item) {
return item % 2 === 0;
// 可以被2整除並等於0的就代表偶數。
})
console.log(newAny)
// [2, 4, 6, 8, 10]
any = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
any.filter(function(item) {
return item % 2 !== 0;
// 不能被2整除並不等於0的就代表基數。
})
// [1, 3, 5, 7, 9]
[1, 2, 3].filter((item, index, arr) => {
arr[i + 1] > 2;
});
// arr[0+1] => arr[1]取出陣列第一位 2 > 2 ? false
// arr[1+1] => arr[2]取出陣列第二位 3 > 2 ? true
// arr[2+1] => arr[3]取出陣列第三位 undefined > 2 ? false
// 丟出 true 的結果
// 回傳 2
tips:console.table
.slice(begin, end)
取得範圍splice
有點類似begin 選擇性
自哪一個索引(起始為 0)開始提取拷貝
end 選擇性
any = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
any.slice(4, 9);
// [5, 6, 7, 8, 9]
// 從 [4]~[9] 不包含第九的索引值,所以沒有 10
any = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let a = any.slice(0);
console.log(a);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.push("9");
console.log(a);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "9"]
.concat([連接陣列])
相連
any = ['A', 'B'];
any.concat(['C', 'D', 'E']);
// ['A', 'B', 'C', 'D', 'E']
.shift()
拿取出第一個不用帶參數
ary = [1, 2, 3, 4];
console.log(ary.shift()); // 1
console.log(ary); // [2, 3, 4]
.unshift(parameter)
放回去第一個
ary = [2, 3, 4];
console.log(ary.unshift(1)); // 現在有 4 筆資料
console.log(ary); // [1, 2, 3, 4]
.pop()
拿取出最後一個拿出陣列中最後一筆資料
.pop() 是不用帶參數
ary = [1, 2, 3, 4];
console.log(ary.pop()); // 4
console.log(ary); // [1, 2, 3]
.push(parameter)
放回去最後一個
ary = [1, 2, 3];
console.log(ary.push(4)); // 現在有 4 筆資料
console.log(ary); // [1, 2, 3, 4]
.splice()
刪除與塞入資料特定位置取出(刪除)陣列特定位置
splice(start,deleteContent)
可以帶入2個參數
start
索引位置(從 0 開始)deleteContent
刪除幾筆資料量 或 取出幾筆資料量
ary = ['A', 'B', 'C', 'D', 'E', 'F'];
console.log(ary.splice(2,1));
// 索引值第二、刪除一筆
// ['C']
console.log(ary);
// ['A', 'B', 'D', 'E', 'F']
splice(start)
從索引值開始刪到最後
ary = ['A', 'B', 'C', 'D', 'E', 'F'];
console.log(ary.splice(2));
// ['C', 'D', 'E', 'F']
console.log(ary);
// ['A', 'B']
塞回陣列特定位置
splice(start,0 , 要加入的值)
第二個參數帶入 0
,這樣就不會刪掉陣列中的值
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
var removed = myFish.splice(2, 0, 'drum');
// myFish 為 ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed 為 [], 沒有元素被刪除
sort
針對陣列去做排序function return
帶入兩個參數sort 可以做排序,但是是使用字串去做排序,
所以如果要針對數字排序,就要記得帶入 function,
any = [14, 30, 35, 42, 5, 16, 70, 98, 19, 100];
any.sort(function(a, b) {
return a > b ? 1 : -1;
// return a < b ? 1 : -1; 可得到由大到小排序
});
console.log(any);
// [5, 14, 16, 19, 30, 35, 42, 70, 98, 100]
any = [14, 30, 35, 42, 5, 16, 70, 98, 19, 100];
any.sort(function(a, b) {
return a - b;
// return b - a; 可得到由大到小排序
});
console.log(any);
// [5, 14, 16, 19, 30, 35, 42, 70, 98, 100]
排列生日,從大到小
帶入兩個人去比較 a b
return 1
return -1
排列上與下的順序
.reverse()
倒序
any = ['A', 'B', 'C', 'D', 'E'];
any.reverse();
console.log(any);
// ['E', 'D', 'C', 'B', 'A']
.length
any = ['A', 'B', 'C', 'D', 'E'];
console.log(any.length);
// 5
改變陣列
any = ['A', 'B', 'C', 'D', 'E'];
any.length = 0;
console.log(any);
// []
any = ['A', 'B', 'C', 'D', 'E'];
any.length = 2;
console.log(any);
// ['A', 'B']
.toString()
陣列轉字串,
隔開
ary = [2, 3, 4, 3,2];
ary.toString();
// "2,3,4,3,2"
.join('可自定符號')
陣列轉字串,
隔開
any = ['A', 'B', 'C', 'D', 'E'];
any.join();
// "A,B,C,D,E"
any = ['A', 'B', 'C', 'D', 'E'];
any.join('_');
// "A_B_C_D_E"
any = ['A', 'B', 'C', 'D', 'E'];
any.join('');
// "ABCDE"
.reduce(function(prev, next){ return}, 初始值)
累加return
),否則會出現 undefined
any = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
any.reduce(function(prev, next){
return prev + next;
});
// 55
帶入的是 字串 會變成字串組合
any = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
any.reduce(function(prev, next){
return prev + next;
});
// "12345678910"
範例:年份加起來總共幾年
這個 array 可以看到一個 inventor 是從 year 到 passed ,所以想要知道經過多久時間,可以 passed - year
(ex: 1955-1879)
0 是累加的初始值,一開始這個 total 是自己設定的,所以要給一個初始才會繼續往上加
.reduceRight(function(prev, next){ return})
通常若使用數字來累積是看不出差異的
any = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
any.reduceRight(function(prev, next){
return prev + next;
});
// 55
// 跟 reduse 得到的答案是一樣的
any = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
any.reduceRight(function(prev, next){
return prev + next;
});
// "10987654321"
.indexOf('搜尋的值')
搜尋陣列的索引位置-1
any = ['A', 'B', 'C', 'D', 'E'];
any.indexOf('B');
// 1
// A位子代表 0,B則為 1
any.indexOf('J');
// -1
.lastIndexOf('搜尋的值')
搜尋陣列的索引位置最後一筆資料
索引位子
any = ['A', 'B', 'A', 'D', 'A'];
any.indexOf('A');
// 0
// 此時會發現indexOf會回傳他第一個找到的A
any.lastIndexOf('A');
// 4
// lastIndexOf他會找到最後一筆,所以最後一筆A在陣列索引4
findIndex()
檢查陣列索引值
const arr=[1,2,3,4,5]
console.log(arr.findIndex(function(e){return e===1}))
// 0
const arr=[1,2,3,4,5]
console.log(arr.findIndex(function(e){return e>1}))
//找到2這個元素,並回傳2的索引號1
.some(function(){})
一個滿足條件,即回傳 true
any = ['C', 'B', 'C', 'C', 'C'];
any.some(function(val) {
return val === 'C'
});
// true
.every(function(){})
全部都要滿足條件,才會回傳 true
any = ['C', 'B', 'C', 'C', 'C'];
any.every(function(val) {
return val === 'C'
});
// false
any = ['A', 'B', 'C', 'D', 'E'];
any.every(function(val) {
return val.length === 1;
});
// true
any = ['A1', 'Bw', 'aC', 'dD', 'aE'];
any.every(function(val) {
return val.length === 2;
});
// true
.forEach(function(item, index, array){})
迴圈.forEach(function(item, index, array){}
可以帶入三個參數(參數名稱可自定義):
item
物件值index
陣列索引array
陣列本身
any = ['A', 'B', 'C', 'D', 'E'];
any.forEach(function(item, index, array) {
console.log(item, index, array);
})
// A 0 (5) ["A", "B", "C", "D", "E"]
// B 1 (5) ["A", "B", "C", "D", "E"]
// C 2 (5) ["A", "B", "C", "D", "E"]
// D 3 (5) ["A", "B", "C", "D", "E"]
// E 4 (5) ["A", "B", "C", "D", "E"]
any = ['A', 'B', 'C', 'D', 'E'];
for (let i = 0; i < any.length; i++){
console.log(any[i],i); // i 代表索引值
}
// A 0
// B 1
// C 2
// D 3
// E 4
JS
總彙整:陣列與物件整合運用
JS