網(wǎng)站開發(fā)成本核算杭州seo公司哪家好
-
push()
- 將一個或多個元素添加到數(shù)組的末尾,并返回新的長度。
let arr = [1, 2, 3]; arr.push(4); // arr 現(xiàn)在是 [1, 2, 3, 4]
pop()
- 刪除并返回數(shù)組的最后一個元素
let arr = [1, 2, 3, 4]; let last = arr.pop(); // last 現(xiàn)在是 4,arr 現(xiàn)在是 [1, 2, 3]
-
shift()
- 刪除并返回數(shù)組的第一個元素。
let arr = [1, 2, 3, 4]; let first = arr.shift(); // first 現(xiàn)在是 1,arr 現(xiàn)在是 [2, 3, 4]
unshift()
- 將一個或多個元素添加到數(shù)組的開頭,并返回新的長度
- 刪除并返回數(shù)組的第一個元素。
- 將一個或多個元素添加到數(shù)組的末尾,并返回新的長度。
let arr = [2, 3, 4];
arr.unshift(1); // arr 現(xiàn)在是 [1, 2, 3, 4]
slice()
- 提取數(shù)組的一部分并在新的數(shù)組中返回。提取的范圍由開始和結(jié)束索引確定。不包含結(jié)束索引
?
let arr = [0, 1, 2, 3, 4];
let sliced = arr.slice(1, 3); // sliced 現(xiàn)在是 [1, 2]
?
splice()
- 通過刪除或替換現(xiàn)有元素或者添加新元素來修改數(shù)組,然后返回被修改的元素
let arr = [0, 1, 2, 3];
arr.splice(1, 2, 'a', 'b'); // arr現(xiàn)在是 [0, 'a', 'b', 3]
?
-
sort()
- 對數(shù)組元素進行排序。默認排序是字母順序或數(shù)字順序。按照字母順序排列字符串時,會先轉(zhuǎn)換為 Unicode 碼點。如果提供了比較函數(shù),則按照該函數(shù)的結(jié)果進行排序。比較函數(shù)應(yīng)該接收兩個參數(shù),如果第一個參數(shù)應(yīng)該位于第二個參數(shù)之前,則返回負數(shù);如果兩個參數(shù)相等,則返回零;如果第一個參數(shù)應(yīng)該位于第二個參數(shù)之后,則返回正數(shù)。
let arr = [3, 1, 4];
arr.sort(); // arr現(xiàn)在是 [1, 3, 4]
reverse()
- 將數(shù)組的元素顛倒順序。請注意,原始數(shù)組會被修改
let arr = [0, 1, 2]; arr.reverse(); // arr現(xiàn)在是 [2, 1, 0]
-
concat()
- 將兩個或更多數(shù)組連接在一起,并返回結(jié)果。不會更改現(xiàn)有數(shù)組,而是返回新數(shù)組。這是連接兩個或更多數(shù)組的一種簡便方法。它不會修改現(xiàn)有的數(shù)組,而是返回一個新數(shù)組。如果參數(shù)中的某些不是數(shù)組,它們會被轉(zhuǎn)換為數(shù)組。如果任何參數(shù)為null或undefined,則它們將被視為空數(shù)組。任何非數(shù)組參數(shù)都不會改變原數(shù)組。這也是一種連接兩個或更多數(shù)組合并成一個新數(shù)組的簡便方法。例如:
let new_array = old_array.concat(value1[, value2[, ...[, valueN]]]);
。它將把所有可迭代的參數(shù)合并到一起,并返回結(jié)果數(shù)組。所以如果要合并兩個字符串(字符串也可以使用該方法)也是可以的,代碼如下:var str1 = 'Hello';
?和?var str2 = 'World';
?通過?str1.concat(str2);
?后會得到結(jié)果字符串?'HelloWorld'
。 注意,該方法并不會把合并的結(jié)果存儲在新的變量里,而是改變了原來的舊變量。如需使用結(jié)果值,需要將結(jié)果賦值給新的變量:var result = str1.concat(str2);
?這樣?result
?的值就是?'HelloWorld'
。
join()
- 將數(shù)組中所有元素連接成一個字符串。需要傳入一個參數(shù)作為連接符,如果不傳則默認為逗號
let arr = ['a', 'b', 'c'];
let str = arr.join(); // str現(xiàn)在是 "a,b,c"
?
-
indexOf()
- 返回指定元素在數(shù)組中的第一個索引,如果不存在則返回-1。
let arr = ['a', 'b', 'c'];
let index = arr.indexOf('b'); // index現(xiàn)在是 1
?
-
lastIndexOf()
- 返回指定元素在數(shù)組中最后一個索引,如果不存在則返回-1。
?
let arr = ['a', 'b', 'c', 'b'];
let index = arr.lastIndexOf('b'); // index現(xiàn)在是 3
?
forEach()
- 對數(shù)組中的每個元素執(zhí)行一次給定的函數(shù)
let arr = [1, 2, 3]; arr.forEach(function(value, index) { console.log(value); // 依次輸出 1, 2, 3 });
map()
- 創(chuàng)建一個新數(shù)組,其結(jié)果是該數(shù)組中的每個元素都調(diào)用一個提供的函數(shù)后的結(jié)果
let arr = [1, 2, 3];
let newArr = arr.map(function(value) { return value * 2; // 返回新數(shù)組 [2, 4, 6]
});
?
filter()
- 創(chuàng)建一個新數(shù)組,包含通過所提供函數(shù)實現(xiàn)的測試的所有元素
let arr = [1, 2, 3, 4];
let newArr = arr.filter(function(value) { return value > 2; // 返回新數(shù)組 [3, 4]
});
?
reduce()
- 對數(shù)組中的每個元素應(yīng)用一個函數(shù),將其減少為單個值
let arr = [1, 2, 3, 4];
let sum = arr.reduce(function(a, b) { return a + b; // 返回 10
}, 0);
?
-
find()
- 返回數(shù)組中滿足提供的測試函數(shù)的第一個元素的值。否則返回 undefined。
let arr = [1, 2, 3, 4];
let value = arr.find(function(value) { return value > 2; // 返回 3
});
?
findIndex()
- 返回數(shù)組中滿足提供的測試函數(shù)的第一個元素的索引。否則返回 -1
let arr = [1, 2, 3, 4];
let index = arr.findIndex(function(value) { return value > 2; // 返回 2
});
?
some()
- 測試數(shù)組中是否至少有一個元素通過由提供的函數(shù)實現(xiàn)的測試
let arr = [1, 2, 3, 4];
let result = arr.some(function(value) { return value > 3; // 返回 true
});
-
every()
- 測試數(shù)組的所有元素是否都通過了由提供的函數(shù)實現(xiàn)的測試。
let arr = [1, 2, 3, 4];
let allPositive = arr.every(function(value) { return value > 0; // 返回 true
});
-
includes()
- 判斷一個數(shù)組是否包含一個指定的值,根據(jù)情況,如果包含則返回 true,否則返回 false。
let arr = [1, 2, 3, 4]; let included = arr.includes(2); // 返回 true
flat()?或?flatten()?(注意:
flatten()
?并不是標準的方法,但經(jīng)常被誤用或混淆;應(yīng)使用?flat()
)
- 判斷一個數(shù)組是否包含一個指定的值,根據(jù)情況,如果包含則返回 true,否則返回 false。
-
- 創(chuàng)建一個新數(shù)組,所有子數(shù)組的元素都遞歸連接到一個新數(shù)組中。
let arr = [1, 2, [3, 4, [5, 6]]];
let flattened = arr.flat(2); // 返回 [1, 2, 3, 4, 5, 6]
-
flatMap()- 先對每個元素執(zhí)行一個映射函數(shù),然后將結(jié)果展平到一個新數(shù)組中。
let arr = [1, 2, 3];
let newArr = arr.flatMap(function(value) { return [value, value * 2]; // 返回 [1, 2, 2, 4, 3, 6]
});
entries()- 返回一個新的數(shù)組迭代器對象,它包含數(shù)組中每個索引的鍵/值對。
let arr = ['a', 'b', 'c'];
let iterator = arr.entries();
for (let entry of iterator) { console.log(entry); // 輸出 [0, 'a'], [1, 'b'], [2, 'c']
}
keys()- 返回一個新的數(shù)組迭代器對象,它包含數(shù)組中每個索引的鍵。
let arr = ['a', 'b', 'c'];
let iterator = arr.keys();
for (let key of iterator) { console.log(key); // 輸出 0, 1, 2
}
values()- 返回一個新的數(shù)組迭代器對象,它包含數(shù)組的每個值。
let arr = ['a', 'b', 'c'];
let iterator = arr.values();
for (let value of iterator) { console.log(value); // 輸出 'a', 'b', 'c'
}
copyWithin()- 在數(shù)組內(nèi)部,將一系列元素復制到另一個位置,覆蓋原有元素,但不會改變數(shù)組大小。
let arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3, 4); // 返回 [4, 2, 3, 4, 5],從索引3復制元素到索引0
fill()- 用一個固定值填充數(shù)組的一部分,從起始索引到結(jié)束索引。
let arr = [1, 2, 3, 4, 5];
arr.fill(0, 1, 3); // 返回 [1, 0, 0, 4, 5],從索引1到索引3(不包括)填充0