Jun

只要我的心還會跳,腿還能動

我就沒有理由停下前進的步伐

常用陣列方法


前言

本篇記錄一些工作上常用到的陣列方法,目的為能夠快速查找與使用,若需要更深入的解釋或範例可至 MDN 閱讀。

文中有些函數為 Pure function,意即操作時不會改變原陣列的內容,也可稱為無副作用,其餘的函數在操作後原陣列皆有可能產生變動。

一些常用到的 ES6 函數如 some()every()map()filter() 的回呼函數內的參數範例只帶第 1 個 element,後面的 indexarray 分別代表各元素索引值與該陣列就不加以贅述與示範,相信用過 forEach() 的人都很孰悉了。


.some()

var arr = [1, 2, 3, 4, 5];

const res1 = arr.some(item => item === 3);
console.log(res1); // true

const res2 = arr.some(item => item === 100);
console.log(res2); // false

.every()

var arr = [1, 2, 3, 4, 5];

const res1 = arr.every(item => item > 0);
console.log(res1); // true

const res2 = arr.every(item => item > 1);
console.log(res2); // false

.map()

var arr = [1, 2, 3, 4, 5];

const res1 = arr.map(item => {});
console.log(res1); // [ undefined, undefined, undefined, undefined, undefined ]

const res2 = arr.map(item => item + 5);
console.log(res2); // [ 6, 7, 8, 9, 10 ]

const res3 = arr.map(item => item > 3);
console.log(res3); // [ false, false, false, true, true ]

const res4 = arr.map(item => {
  if (item > 3) return item;
});
console.log(res4); // [ undefined, undefined, undefined, 4, 5 ]

.filter()

var arr = [1, 2, 3, 4, 5];

const res1 = arr.filter(item => {});
console.log(res1); // []

const res2 = arr.filter(item => item > 2);
console.log(res2); // [ 3, 4, 5 ]

.slice()

var arr = [1, 2, 3, 4, 5];

const res1 = arr.slice(3);
console.log(res1); // [ 4, 5 ]

const res2 = arr.slice(2, 4);
console.log(res2); // [ 3, 4 ]

const res3 = arr.slice(1, -1);
console.log(res3); // [ 2, 3, 4 ]

.concat()

var arr = [1, 2, 3, 4, 5];
var str = 'Hello World';
var obj = { name: 'Jun' };
var arr2 = [6, 7, 8];

const res1 = arr.concat(str);
console.log(res1); // [ 1, 2, 3, 4, 5, 'Hello World' ]

const res2 = arr.concat(obj);
console.log(res2); // [ 1, 2, 3, 4, 5, { name: 'Jun' } ]

const arr3 = arr.concat(arr2);
console.log(arr3); // [ 1, 2, 3, 4, 5, 6, 7, 8 ]

.sort()

var arr = [100, 25, 75, 12500, 50, 600];

const res1 = arr.sort((x, y) => (x > y ? 1 : -1));
console.log(res1); // [ 25, 50, 75, 100, 600, 12500 ]

const res2 = arr.sort((x, y) => (x < y ? 1 : -1));
console.log(res2); // [ 12500, 600, 100, 75, 50, 25 ]

.shift()

var arr = [1, 2, 3, 4, 5];

console.log(ary.shift()); // 1
console.log(ary); // [ 2, 3, 4, 5 ]

var empty = [];
console.log(empty.shift()); // undefined

.unshift()

var arr = [1, 2, 3, 4, 5];

console.log(arr.unshift('Hello', 'World')); // 7
console.log(arr); // [ 'Hello', 'World', 1, 2, 3, 4, 5 ]

.pop()

var arr = [1, 2, 3, 4, 5];

console.log(arr.pop()); // 5
console.log(arr); // [ 1, 2, 3, 4 ]

var empty = [];
console.log(empty.pop()); // undefined

.push()

var arr = [1, 2, 3, 4, 5];

console.log(arr.push('Hello', 'World')); // 7
console.log(arr); // [ 1, 2, 3, 4, 5, 'Hello', 'World' ]

.splice()

var arr = [1, 2, 3, 4, 5];

console.log(arr.splice(2, 1, 'Hello')); // [ 3 ]
console.log(arr); // [ 1, 2, 'Hello', 4, 5 ]

.join()

var arr = [1, 2, 3, 4, 5];

console.log(arr.join()); // 1,2,3,4,5
console.log(arr.join('')); // 12345
console.log(arr.join('、')); // 1、2、3、4、5
console.log(arr); // [ 1, 2, 3, 4, 5 ]

.indexOf()

var arr = [1, 2, 3, 4, 5];

console.log(arr.indexOf(3)); // 2
console.log(arr.indexOf(3, 3)); // -1
console.log(arr); // [ 1, 2, 3, 4, 5 ]

.lastIndexOf()

var arr = [1, 2, 3, 4, 5, 2, 3];

console.log(arr.lastIndexOf(2)); // 5
console.log(arr.lastIndexOf(2, -2)); // 5
console.log(arr.lastIndexOf(2, -3)); // 1
console.log(arr); // [ 1, 2, 3, 4, 5, 2, 3 ]

.reverse()

var arr = [1, 2, 3, 4, 5];

console.log(arr.reverse()); // [ 5, 4, 3, 2, 1 ]
console.log(arr); // [ 5, 4, 3, 2, 1 ]