元問答欄目視頻美女
  1. 編程問答
  2. 答案列表

JavaScript 數組操作方法大全

回答列表
javascript中的array對象與其他程式語言中的數組一樣,可以將多個項目的集合存儲在單個變量名下,並具有用於執行常見數組操作的成員。
聲明數組
有兩種不同的方式可以聲明數組。
使用new array
通過new array,我們可以指定希望存在於數組中的元素,如下所示:
const fruits = new array('apple','banana');
console.log(fruits.length);
數組字面量表示法。
使用數組字面量聲明,我們可以指定數組將具有的值。如果我們不聲明任何值,則數組將為空。
// 通過數組字面量創建一個有2個元素的'fruits'數組。
const fruits = ['apple','banana'];
console.log(fruits.length);
以下是array對象的方法列表及描述。
1.foreach
foreach()方法將為每個數組元素執行一次指定的函數。
foreach()為數組中的每個元素按索引升序調用提供的callbackfn函數一次。它不會為已刪除或未初始化的索引屬性調用。
array.foreach(callback[,thisobject]);
const array1 = ['a','b','c'];
array1.foreach(element =>console.log(element));
// expected output:"a"
// expected output:"b"
// expected output:"c"
2.map
array.map()方法允許你遍曆數組並使用回調函數修改其元素。然後將在數組的每個元素上執行回調函數。
array.map(callback[,thisobject]);
let arr = [3,4,5,6];
let modifiedarr = arr.map(function(element){
return element *3;
});
console.log(modifiedarr);// [9,12,15,18]。
array.map()方法通常用於對元素應用一些更改,無論是像在上面代碼中那樣乘以特定數字,還是執行應用程式可能需要的任何其他操作。
3.concat
javascript中的concat()方法是一個字符串方法,用於將字符串連接在一起。concat()方法將一個或多個字符串值附加到調用字符串,然後將連接的結果作為新字符串返回。因為concat()方法是string對象的方法,所以必須通過string類的特定實例來調用它。
array.concat(value1,value2,...,valuen);
const array1 = ['a','b','c'];
const array2 = ['d','e','f'];
const array3 = array1.concat(array2);
console.log(array3);
// expected output:array ["a","b","c","d","e","f"]。
4.push
javascript數組中的push()方法將給定元素附加到數組最後並返回新數組的長度。
如果你想在數組末尾添加一個元素,請使用push()
array.push(element1,...,elementn);
const countries = ["nigeria","ghana","rwanda"];
countries.push("kenya");
console.log(countries);// ["nigeria","ghana","rwanda","kenya"]。
5.pop
pop()方法將刪除數組的最後一個元素並將該值返回給調用者。如果你在空數組上調用pop(),則返回undefined
array.prototype.shift()與pop()具有相似的行為,但應用於數組中的第一個元素。
array.pop();
const plants = ['broccoli','cauliflower','cabbage','kale','tomato'];
console.log(plants.pop());
// expected output:"tomato"
console.log(plants);
// expected output:array ["broccoli","cauliflower","cabbage","kale"]。
6.splice
splice()方法是一種通用方法,用於在數組的指定位置通過刪除、替換或添加元素來更改數組的內容。本節將介紹如何使用此方法將元素添加到特定位置。
array.splice(index,howmany,[element1][,...,elementn]);
const fruits = ["banana","orange","apple","mango"];
fruits.splice(2,0,"lemon","kiwi");//banana,orange,lemon,kiwi,apple,mango
7.slice
slice()方法將一部分數組的淺表副本返回到從開始到結束(不包括結束)選擇的新數組對象中,其中開始和結束表示該數組中項目的索引。該方法不會修改原始數組。
array.slice( begin [,end] );
const animals = ['ant','bison','camel','duck','elephant'];
console.log(animals.slice(2));
// expected output:array ["camel","duck","elephant"]。
console.log(animals.slice(2,4));
// expected output:array ["camel","duck"]。
8.shift
shift()是內置的javascript函數,用於從數組中刪除第一個元素。shift()函數直接修改正在使用的數組。同時shift()返回數組中刪除的項目。
shift()函數刪除索引位置0的項目,並將索引號的值依次向下移動1。
array.shift();
const array1 = [1,2,3];
const firstelement = array1.shift();
console.log(array1);
// expected output:array [2,3]。
console.log(firstelement);
// expected output:1。
9.unshift
unshift()方法將插入給定值到類數組對象的開頭。
array.prototype.push()與unshift()具有相似的行為,但應用於數組的末尾。
array.unshift( element1,...,elementn );
const array1 = [1,2,3];
console.log(array1.unshift(4,5));
// expected output:5。
console.log(array1);
// expected output:array [4,5,1,2,3]。
10.join
javascript數組中的join()方法是一個內置方法,通過連接數組的所有元素來創建並返回新字符串。join()方法將連接數組的項到字符串並返回該字符串。指定的分隔符用於分隔元素數組。默認分隔符是逗號(,)
array.join(separator);
const elements = ['fire','air','water'];
console.log(elements.join());
// expected output:"fire,air,water"
console.log(elements.join(''));
// expected output:"fireairwater"
console.log(elements.join('-'));
// expected output:"fire-air-water"
11.every
every()方法測試數組中的所有元素是否都滿足指定的條件。返回的是布爾值。
array.every(callback[,thisobject]);
const isbelowthreshold = (currentvalue) =>currentvalue <40;
const array1 = [1,30,39,29,10,13];
console.log(array1.every(isbelowthreshold));
// expected output:true
12.filter
filter()方法創建部分給定數組的淺表副本,向下過濾到給定數組中的元素,且元素通過所提供函數實現的條件測試。
array.filter(callback[,thisobject]);
const words = ['spray','limit','elite','exuberant','destruction','present'];
const result = words.filter(word =>word.length >6);
console.log(result);
// expected output:array ["exuberant","destruction","present"]。
13.indexof
indexof()方法返回可以在數組中找到給定元素的第一個索引,如果不存在則返回-1。
array.indexof(searchelement[,fromindex]);
const beasts = ['ant','bison','camel','duck','bison'];
console.log(beasts.indexof('bison'));
// expected output:1。
// start from index 2。
console.log(beasts.indexof('bison',2));
// expected output:4。
console.log(beasts.indexof('giraffe'));
// expected output:-1。
14.reduce
reduce()方法按順序對數組的每個元素執行用戶提供的reducer回調函數,傳入前一個元素的計算返回值。在數組的所有元素上運行reducer的最終結果是單個值。
array.reduce(callback[,initialvalue]);
const array1 = [1,2,3,4];
// 0 + 1 + 2 + 3 + 4。
const initialvalue = 0;
const sumwithinitial = array1.reduce(
(previousvalue,currentvalue) =>previousvalue + currentvalue
initialvalue、);
console.log(sumwithinitial)
15.reverse
reverse()方法將反轉數組並返回對相同數組的引用,第一個數組元素成為最後一個,最後一個數組元素成為第一個。換句話說,數組中的元素順序將轉向與之前相反的方向。
array.reverse();
const array1 = ['one','two','three'];
console.log('array1:',array1);
// expected output:"array1:" array ["one","two","three"]。
const reversed = array1.reverse();
console.log('reversed:',reversed);
// expected output:"reversed:" array ["three","two","one"]。
// careful:reverse is destructive -- it changes the original array
console.log('array1:',array1);
// expected output:"array1:" array ["three","two","one"]。
16.sort
sort()方法對數組的元素進行就地排序,並返回對同一個數組的引用,而此時數組已排序。默認排序順序是升序,將元素轉換為字符串,然後比較它們的utf-16代碼單元值序列。
array.sort( comparefunction );
const months = ['march','jan','feb','dec'];
months.sort();
console.log(months);
// expected output:array ["dec","feb","jan","march"]。
const array1 = [1,30,4,21,100000];
array1.sort();
console.log(array1);
// expected output:array [1,100000,21,30,4]。
17.tostring
tostring()方法返回表示對象的字符串。
array.tostring();
function dog(name) {
this.name = name;
}
const dog1 = new dog('gabby');
dog.prototype.tostring = function dogtostring() {
return `${this.name}`;
};
console.log(dog1.tostring());
// expected output:"gabby"
18.at
at()方法接受整數值並返回at索引的項目,正整數和負整數皆可。負整數從數組中的最後一項開始倒數。
array.at(index)
const array1 = [5,12,8,130,44];
let index = 2;
console.log(`using an index of ${index} the item returned is ${array1.at(index)}`);
// expected output:"using an index of 2 the item returned is 8"
index = -2;
console.log(`using an index of ${index} item returned is ${array1.at(index)}`);
// expected output:"using an index of -2 item returned is 130"
19.find
find()方法返回數組中滿足條件測試函數的第一個元素。如果沒有值滿足提供的測試函數,則返回undefined
array.find(function(currentvalue,index,arr),thisvalue)
const array1 = [5,12,8,130,44];
const found = array1.find(element =>element >10);
console.log(found);
// expected output:12。
20.some
some()方法測試數組中是不是至少有一個元素通過了函數實現的條件測試。如果在數組中找到這樣的元素就返回true;否則返回false。該方法不修改原數組。
array.some(callback[,thisobject]);
const array = [1,2,3,4,5];
// checks whether an element is even
const even = (element) =>element % 2 === 0;
console.log(array.some(even));
// expected output:true
猜你喜歡
相關推薦