在前面的基礎介紹中我們有介紹過陣列(array),它是有順序性的集合。今天來介紹一些基本操作陣列的方法吧!
Array.prototype.push(elementN)
push()這個方法,能夠在原本的array「最後面」加上新的值。讓我們來看看範例:
const fruit = ["apple", "orange", "banana"];
const newFruit = fruit.push("cherry");
console.log(fruit); //["apple", "orange", "banana", "cherry"]
console.log(newFruit); //4
從上面範例還能發現,使用push()之後,除了在原本的陣列加上新值,還會回傳這個陣列的長度。
Array.prototype.unshift(elementN)
unshift()跟push()很像,它是能夠在原本陣列的「最前面」,加上新值並且回傳陣列長度。
const fruit = ["apple", "orange", "banana"];
const newFruit = fruit.unshift("cherry");
console.log(fruit); //["cherry","apple", "orange", "banana"]
console.log(newFruit); //4
Array.prototype.concat()
concat()這個方法可以將兩個陣列合併在一起,回傳新的陣列。原本的陣列不會改變。
const fruit1 = ["apple", "orange", "banana"];
const fruit2 = ["cherry","peach","papaya"];
const allFruit = fruit1.concat(fruit2);
console.log(fruit1); // ["apple", "orange", "banana"];
console.log(allFruit); // ["apple", "orange", "banana", "cherry","peach","papaya"]
Array.prototype.pop()
移除原本陣列「最後面」的值,回傳被移除掉的那個值。
const fruit = ["apple", "orange", "banana"];
const delFruit = fruit.pop();
console.log(fruit); //["apple", "orange"]
console.log(delFruit); //"banana"
Array.prototype.shift()
移除原本陣列「最前面」的值,回傳被移除掉的那個值。
const fruit = ["apple", "orange", "banana"];
const delFruit = fruit.shift();
console.log(fruit); //["orange","banana"]
console.log(delFruit); //"apple"
接下來的方法都會用這個陣列作為範例:
const inventors = [
{ first: "Albert", last: "Einstein", year: 1879, passed: 1955 },
{ first: "Isaac", last: "Newton", year: 1643, passed: 1727 },
{ first: "Galileo", last: "Galilei", year: 1564, passed: 1642 },
{ first: "Marie", last: "Curie", year: 1867, passed: 1934 },
{ first: "Johannes", last: "Kepler", year: 1571, passed: 1630 },
{ first: "Nicolaus", last: "Copernicus", year: 1473, passed: 1543 },
{ first: "Max", last: "Planck", year: 1858, passed: 1947 },
{ first: "Katherine", last: "Blodgett", year: 1898, passed: 1979 },
{ first: "Ada", last: "Lovelace", year: 1815, passed: 1852 },
{ first: "Sarah E.", last: "Goode", year: 1855, passed: 1905 },
{ first: "Lise", last: "Meitner", year: 1878, passed: 1968 },
{ first: "Hanna", last: "Hammarström", year: 1829, passed: 1909 }
];
Array.prototype.filter()
// 篩選出於1500~1599年間出生的inventor
let result = inventors.filter(function(inventor){
return inventor.year >= 1500 && inventor.year < 1600
})
console.log(result);
//[{first: "Galileo", last: "Galilei", passed: 1642, year: 1564}, {first: "Johannes", last: "Kepler", passed: 1630, year: 1571}]
filter()這個方法接收一個function,它回傳結果為true 的值,並重新組成陣列。
Array.prototype.map()
//將inventors內的first與last組合成一個陣列
const fullNames = inventors.map(function(inventor){
return `${inventor.first} ${inventor.last}`;
} )
console.log(fullNames) //["Albert Einstein", "Isaac Newton", "Galileo Galilei", "Marie Curie", "Johannes Kepler", "Nicolaus Copernicus", "Max Planck", "Katherine Blodgett", "Ada Lovelace", "Sarah E. Goode", "Lise Meitner", "Hanna Hammarström"]
map()這個方法一樣接收一個function,並對陣列的每個元素做操作,然後將結果重新組合成跟原本陣列數量一樣的陣列。
Array.prototype.reduce()
// 加總所有inventor的在世時間
const totalYears = inventors.reduce(function(accumulator, inventor){
return accumulator + (inventor.passed - inventor.year)
}, 0)
console.log(totalYears) //861
reduce()這個方法接收兩個參數,第一個是function,第二個是起始值(initialValue ),而function有四個參數:
我們用比較簡單的範例來觀察一下:
const array = [0,1,2,3,4]
let total = array.reduce(function(accumulator,currentValue,currentIndex, array){
return accumulator + currentValue
})
console.log(total)
accumulator | currentValue | currentIndex | array | 回傳的值 | |
---|---|---|---|---|---|
第一次 | 0 | 1 | 1 | [0, 1, 2, 3, 4] | 1 |
第二次 | 1 | 2 | 2 | [0, 1, 2, 3, 4] | 3 |
第三次 | 3 | 3 | 3 | [0, 1, 2, 3, 4] | 6 |
第四次 | 6 | 4 | 4 | [0, 1, 2, 3, 4] | 10 |
以上就是陣列的基本方法介紹,陣列常見的方法還有非常多,例如:sort()、find()、forEach()等等,之後解題如果有用到會再介紹。