十大必知 JavaScript 函数!十大必知 JavaScript 函数

2025-06-07

十大必须知道的 JavaScript 函数!

十大必知 JavaScript 函数

我最近开通了一个新的博客TheDailyTechTalk,在这里创作免费内容。如果你喜欢这篇文章,并且想阅读更多关于 JavaScript 的文章,欢迎访问 🎉🎉
🥰

如果您喜欢我写的内容并希望支持我,请在Twitter上关注我,以了解有关编程和类似主题的更多信息 ❤️❤️

十大必知 JavaScript 函数

请在此处查看全文

1 过滤器()

此函数根据您提供的条件过滤数组,并返回包含满足这些条件的项目的新数组。

filter()

const temperatures = [10, 2, 30.5, 23, 41, 11.5, 3];

const coldDays = temperatures.filter(dayTemperature => {
    return dayTemperature < 20;
});

console.log("Total cold days in week were: " + coldDays.length); // 4
Enter fullscreen mode Exit fullscreen mode

2 地图()

函数map()非常简单,它循环遍历数组并将每个项目转换为其他内容。

const readings = [10, 15, 22.5, 11, 21, 6.5, 93];
const correctedReadings = readings.map(reading => reading + 1.5);
console.log(correctedReadings); // gives [11.5, 16.5, 24, 12.5, 22.5, 8, 94.5]
Enter fullscreen mode Exit fullscreen mode

3 一些()

some()与非常相似filter(),但some()返回布尔值。

const animals = [
    {
        name: 'Dog',
        age: 2
    },

    {
        name: 'Cat',
        age: 8
    },

     {
        name: 'Sloth',
        age: 6
    },
];

if(animals.some(animal => {
    return animal.age > 4
})) {
    console.log("Found some animals!")
}
Enter fullscreen mode Exit fullscreen mode

4 每个()

every()也与非常相似some(),但every()仅当数组中的每个元素都满足我们的条件时才为真。

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));   // true
Enter fullscreen mode Exit fullscreen mode

5 移位()

shift()方法从数组中删除第一个元素并返回被删除的元素。此方法会更改数组的长度。

const items = ['meat', 'carrot', 'ham', 'bread', 'fish'];
items.shift()
console.log(items); // ['carrot', 'ham', 'bread', 'fish']
Enter fullscreen mode Exit fullscreen mode

6 取消移位()

就像shift()方法从数组中删除第一个元素unshift()并添加它一样。此方法更改数组的长度并返回数组的新长度作为结果。

const items = ['milk', 'fish'];
items.unshift('cookie')
console.log(items); // ['cookie', 'milk', 'fish']
Enter fullscreen mode Exit fullscreen mode

7 切片()

slice()方法将数组的一部分内容浅拷贝至一个新数组对象,该新数组对象从起始位置到结束位置(不包括结束位置),起始位置和结束位置代表该数组中项目的索引。原始数组不会被修改。

let message = "The quick brown fox jumps over the lazy dog";
let startIndex = message.indexOf('brown');
let endIndex = message.indexOf('jumps');
let newMessage = message.slice(startIndex, endIndex);
console.log(newMessage); // "brown fox "
Enter fullscreen mode Exit fullscreen mode

8 拼接()

splice()下面的代码从数组的索引 2(第三个位置,计数从 0 开始!)开始,并删除一个元素。
在我们的数组中,这意味着“兔子”被删除了。splice()将返回新的数组作为结果。

const animals = ['dog', 'cat', 'rabbit', 'shark', 'sloth'];
animals.splice(2, 1);
console.log(animals); // ["dog", "cat", "shark", "sloth"]
Enter fullscreen mode Exit fullscreen mode

9 包括()

includes()将检查数组中的每个项目,并检查其中是否有符合条件的项目。它将返回布尔值。

const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));  //  true

console.log(pets.includes('at'));  //  false
Enter fullscreen mode Exit fullscreen mode

10 反向()

reverse()方法反转数组。请注意,该方法reverse()具有破坏性,这意味着它会改变原始数组。

const array1 = ['one', 'two', 'three', 'four'];
console.log(array1);  //  ["one", "two", "three", "four"]

const reversed = array1.reverse();
console.log(reversed);  //  ["four", "three", "two", "one"]
Enter fullscreen mode Exit fullscreen mode

我最近开通了一个新的博客TheDailyTechTalk,在这里创作免费内容。如果你喜欢这篇文章,并且想阅读更多关于 JavaScript 的文章,欢迎访问 🎉🎉
🥰

如果您喜欢我写的内容并希望支持我,请在Twitter上关注我,以了解有关编程和类似主题的更多信息 ❤️❤️

文章来源:https://dev.to/thedailytechtalk/top-10-must-know-javascript-functions-1ipm
PREV
使用哈希或 URL 进行单页应用程序路由
NEXT
JavaScript 类入门