find()
查找元素,返回符合测试条件的第一个数组元素值,如果没有符合条件则返回undefined(重点)
<script>
const obj = [
{name: '小米', price: '2999'},
{name: '华为', price: '3999'},
]
const mi = obj.find(function(item){
console.log(item);
return item.name === '小米'
})
console.log(mi);
</script>
findIndex()
查找元素的索引值 返回第一个符合的索引值
<script>
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = array1.findIndex(element => element > 13)
console.log(isLargeNumber);// Expected output: 3
</script>
every()
检测数组所有元素是否符合指定条件,如果所有元素都通过检测true,否则返回false(重点)
<script>
//every
const arr1 = [10,20,30]
const flag = arr1.every(item => item >= 10)
console.log(flag); //true
</script>
some()
检测数组中的元素是否满足指定条件 (只要有一个元素满足),如果数组中有满足条件返回true,否则返回false
<script>
//every
const arr1 = [10,20,30]
const flag = arr1.some(item => item >= 20)
console.log(flag); //true
</script>
concat()
合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组
<script>
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"]
</script>
sort()
对数组的元素进行排序,并返回对相同数组的引用。默认排序是将元素转换为字符串,然后按照它们的 UTF-16 码元值升序排序。
由于它取决于具体实现,因此无法保证排序的时间和空间复杂度。
如果想要不改变原数组的排序方法,可以使用 toSorted()。
<script>
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);// ["Dec", "Feb", "Jan", "March"]
const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);// [1, 100000, 21, 30, 4]
</script>
splice()
方法通过移除或者替换已存在的元素和/或添加新元素改变一个数组的内容
要创建一个删除和/或替换部分内容而不改变原数组的新数组,请使用 toSplice()。
<script>
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb'); //移除索引1之前的0个元素
// Inserts at index 1
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, 'May'); 移除索引4之前的1个元素
// Replaces 1 element at index 4
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "May"]
</script>
reverse()
反转数组
<script>
const reversed = array1.reverse();
console.log('reversed:', reversed);// ["three", "two", "one"]
</script>
indexOf
indexOf()
方法返回数组中第一次出现给定元素的下标,如果不存在则返回 -1。
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