JavaScript の一般的な配列操作メソッド

次のメソッドは、呼び出された元の配列の値を変更します。

押す()

Push() メソッドは、1 つ以上の要素を配列の末尾に追加し、配列の新しい長さを返します。

// 语法:arr.push(element1, ..., elementN)
let arr = [1, 2, 3];
let count = arr.push(4);
console.log(count); // 4
console.log(arr); // [1, 2, 3, 4]

ポップ()

Pop() メソッドは、配列から最後の要素を削除し、その要素の値を返します。このメソッドは配列の長さを変更します。

// 语法:arr.pop()
let arr = [1, 2, 3, 4];
let count = arr.pop();
console.log(count); // 4
console.log(arr); // [1, 2, 3]

シフト()

SHIFT() メソッドは、配列から最初の要素を削除し、その要素の値を返します。このメソッドは配列の長さを変更します。

// 语法:arr.shift()
let arr = [1, 2, 3, 4];
let count = arr.shift();
console.log(count); // 1
console.log(arr); // [2, 3, 4]

シフト解除()

unshift() メソッドは、配列の先頭に 1 つ以上の要素を追加し、配列の新しい長さを返します (このメソッドは元の配列を変更します)。

// 语法:arr.unshift(element1, ..., elementN)
let arr = [1, 2, 3, 4];
let count = arr.unshift(0);
console.log(count); // 5
console.log(arr); // [0, 1, 2, 3, 4]

スプライス()

splice() メソッドは、既存の要素を削除または置換するか、新しい要素を所定の位置に追加することによって配列を変更し、変更された内容を配列の形式で返します。このメソッドは元の配列を変更します。

// 语法:array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
let arr = [1, 2, 3, 4];
arr.splice(1, 0, 5);
console.log(arr); // [1, 5, 2, 3, 4]
arr.splice(4, 1, 6);
console.log(arr); // [1, 5, 2, 3, 6]
arr.splice(4, 1, 6, 7, 8);
console.log(arr); // [1, 5, 2, 3, 6, 7, 8]

逆行する()

reverse() メソッドは、配列内の要素の位置を反転し、配列を返します。配列の最初の要素が最後になり、配列の最後の要素が最初になります。このメソッドは元の配列を変更します。

// 语法:arr.reverse()
let arr1 = [1, 2, 3];
let arr2 = arr1.reverse();
console.log(arr2); // [3, 2, 1]
console.log(arr1); // [3, 2, 1]

埋める()

fill() メソッドは、配列内の開始インデックスから終了インデックスまでのすべての要素を固定値で埋めます。終端インデックスは含まれません。

// 语法:arr.fill(value[, start[, end]])
let arr = [1, 2, 3, 4];
arr.fill(0, 2, 4)
console.log(arr); // [1, 2, 0, 0];

copyWithin()

copyWithin() メソッドは、配列の一部を同じ配列内の別の場所に浅くコピーし、元の配列の長さを変更せずにそれを返します。

// 语法:arr.copyWithin(target[, start[, end]])
let arr1 = [1, 2, 3, 4];
let arr2 = arr1.copyWithin(0, 3, 4);
console.log(arr2); // [4, 2, 3, 4]
console.log(arr1); // [4, 2, 3, 4]

以下のメソッドは、呼び出されたオブジェクトの値を変更することはなく、新しい配列またはその他の期待される値を返すだけです。

concat()

concat() メソッドは、2 つ以上の配列を結合するために使用されます。このメソッドは既存の配列を変更しませんが、新しい配列を返します。

// 语法:var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = arr1.concat(arr2);

console.log(arr3); // [1, 2, 3, 4, 5, 6]
console.log(arr1); // [1, 2, 3]
console.log(arr2); // [4, 5, 6]

含む()

include()メソッドは、配列に指定された値が含まれているかどうかを判定するメソッドで、含まれていればtrue、含まれていない場合はfalseを状況に応じて返します。

// 语法:arr.includes(valueToFind[, fromIndex])
const arr = [1, 2, 3, 4, 5];
const bool1 = arr.includes(1);
const bool2 = arr.includes(0);
const bool3 = arr.includes(1, 2);
console.log(bool1); // true
console.log(bool2); // false
console.log(bool3); // false

参加する()

join() メソッドは、配列 (または配列のようなオブジェクト) のすべての要素を文字列に結合し、その文字列を返します。配列に項目が 1 つだけある場合、その項目は区切り文字なしで返されます。

// 语法:arr.join([separator])
const arr1 = [10, 30, 50];
const time = arr1.join(':');
console.log(time); // 10:30:50

スライス()

lice() メソッドは、begin と end (begin を含み、end を除く) によって決定される元の配列の浅いコピーである新しい配列オブジェクトを返します。元の配列は変更されません。

// 语法:arr.slice([begin[, end]])
const arr1 = [1, 2, 3, 4, 5];
const arr2 = arr1.slice(2);
const arr3 = arr1.slice(1,3);
console.log(arr2); // [3, 4, 5]
console.log(arr3); // [2, 3]
console.log(arr1); // [1, 2, 3, 4, 5]

の指標()

IndexOf() メソッドは、指定された要素が見つかる配列内の最初のインデックスを返します。要素が存在しない場合は -1 を返します。

// 语法:arr.indexOf(searchElement[, fromIndex])
const arr = [1, 2, 3, 4, 5];
const index = arr.indexOf(4);
console.log(index) // 3

lastIndexOf()

lastIndexOf() メソッドは、指定された要素 (つまり、有効な JavaScript 値または変数) の配列内の最後のインデックスを返し、それが存在しない場合は -1 を返します。fromIndex から始めて、配列の後ろから前方を見てください。

// 语法:arr.lastIndexOf(searchElement[, fromIndex])
const arr = [1, 2, 5, 3, 4, 5];
const index1 = arr.lastIndexOf(5);
const index2 = arr.lastIndexOf(5, 4);
console.log(index1) // 5
console.log(index2) // 2

反復法

forEach()

forEach() メソッドは、配列の各要素に対して指定された関数を 1 回実行します。

// 语法:arr.forEach(callback(currentValue [, index [, array]])[, thisArg])
const arr = [1, 2, 3];
arr.forEach(item => console.log(item));
// 1
// 2
// 3

地図()

map() メソッドは新しい配列を作成します。その結果、配列内の各要素は、指定された関数への 1 回の呼び出しの戻り値になります。

// 语法:var new_array = arr.map(function callback(currentValue[, index[, array]]) {
    
    
 // Return element for new_array 
// }[, thisArg])
const arr1 = [1, 2, 3];
const arr2 = arr1.map(x => x * 2);
console.log(arr2); // [2, 4, 6]

フィルター()

filter() メソッドは、提供された関数によって実行されるテストに合格したすべての要素を含む新しい配列を作成します。

// 语法:var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
const arr1 = [1, 2, 3, 4, 5];
const arr2 = arr1.filter(x => x > 2);
console.log(arr2); // [3, 4, 5]

毎()

Every() メソッドは、配列内のすべての要素が指定された関数のテストに合格するかどうかをテストします。ブール値を返します。

// 语法:arr.every(callback(element[, index[, array]])[, thisArg])
const arr1 = [1, 2, 3, 4, 5];
const bool1 = arr1.every(x => x > 2);
const bool2 = arr1.every(x => x < 6);
console.log(bool1); // false
console.log(bool2); // true

いくつかの()

some() メソッドは、配列内の少なくとも 1 つの要素が提供された関数テストに合格するかどうかをテストします。ブール型の値を返します。

// 语法:arr.some(callback(element[, index[, array]])[, thisArg])
const arr1 = [1, 2, 3, 4, 5];
const bool1 = arr1.some(x => x > 2);
const bool2 = arr1.some(x => x > 6);
console.log(bool1); // true
console.log(bool2); // false

探す()

find() メソッドは、指定されたテスト関数を満たす配列内の最初の要素の値を返します。それ以外の場合は、未定義を返します。

// 语法:arr.some(callback(element[, index[, array]])[, thisArg])
const arr1 = [1, 2, 3, 4, 5];
const count1 = arr1.find(x => x > 2);
const count2 = arr1.find(x => x > 6);
console.log(count1); // 3
console.log(count2); // undefined。

findIndex()

findIndex() メソッドは、提供されたテスト関数を満たす配列内の最初の要素の値を返します。それ以外の場合は、未定義を返します。

// 语法:arr.findIndex(callback[, thisArg])
const arr1 = [1, 2, 3, 4, 5];
const index1 = arr1.findIndex(x => x > 2);
const index2 = arr1.findIndex(x => x > 6);
console.log(index1); // 2
console.log(index2); // -1

減らす()

reduce() メソッドは、配列内の各要素に対して指定されたリデューサー関数を (昇順で) 実行し、その結果を 1 つの戻り値に集約します。

// 语法:arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
const arr = [1, 2, 3, 4, 5];
const res = arr.reduce((acc, cur) => acc + cur);
console.log(res); // 15

おすすめ

転載: blog.csdn.net/Gage__/article/details/115494803