首页 > web前端 > js教程 > 正文

简短而有趣的 JavaScript 片段

王林
发布: 2024-07-19 12:50:43
转载
721人浏览过

简短而有趣的 javascript 片段

javascript 是一种极其通用且功能强大的编程语言,广泛用于 web 开发。无论您是经验丰富的开发人员还是新手,拥有一组方便的 javascript 代码片段都可以节省您的时间并简化您的编码过程。在本文中,我编译了 15 个简短而精彩的 javascript 片段,涵盖了各种任务。让我们潜入吧!

01.获取当前数据和时间

const currentdatetime = new date();
console.log(currentdatetime);
登录后复制

02. 查找数组中的最大数

const number = [5, 2, 7, 10, 1]
const maxnumber = math.max(...number)

// 10
登录后复制

03. 随机排列数组

function shufflearray(array) {
    return array.sort(() => math.random() - 0.5);
}
登录后复制

04.生成1到10之间的随机数

const randomnumber = math.floor(math.random() * 10) + 1;
console.log(randomnumber);

// 7
登录后复制

05. 将字符串转换为小写

const str = 'hello, world!''
console.log(str.tolowercase());

// hello, world!
登录后复制

06. 检查是偶数还是奇数

const num = 5;
if (num % 2 === 0) {
    console.log('number is even');
} else {
    console.log('number is odd');
}

// 'number is odd'
登录后复制

07. 创建一个简单的 10 秒倒计时器

let seconds = 5;
const countdown = setinterval(() => {
    console.log(seconds);
    seconds--;
    if (seconds < 0) {
        clearinterval(countdown);
        console.log('countdown finished!');
    }
}, 1000);


// 5
// 4
// 3
// 2
// 1
// countdown finsihed!
登录后复制

08. 将数字数组转换为字符串数组

const numbers = [1, 2, 3, 4, 5];
const strings = numbers.map(string);
console.log(strings);

// ['1', '2', '3', '4', '5']
登录后复制

09.删除重复项

let arr = ["apple", "mango", "apple", "orange", "mango", "mango"];
const removeduplicates = arr => [...new set(arr)];

console.log(removeduplicates(arr));

// ['apple', 'mango', 'orange']
登录后复制

10. 将句子转换为单词数组

const sentence = "this is a sentence";
const words = sentence.split(" ");

console.log(words);

// ['this', 'is', 'a', 'sentence']
登录后复制

11. 重复字符串

function repeatstring(str, n){
    return str.repeat(n);
}

const repeatedstr = ('abc', 3);
console.log(repeatedstr);

// 'abcabcabc'
登录后复制

12. 求数组的交集

// define the intersection function
const intersection = (a, b) => a.filter(value => b.includes(value));

// example arrays
const arraya = [1, 2, 3, 4, 5];
const arrayb = [4, 5, 6, 7, 8];

// use the intersection function to find common elements
const result = intersection(arraya, arrayb);

// log the result to the console
console.log(result);

// [4, 5]
登录后复制

13. 创建动态字符串

const name = 'matin imam';
const greeting = `hello, ${name}!`;
console.log(greeting); // "hello, matin imam!"
登录后复制

14. 合并对象

const person = {name: 'matin'};
const details = {work: 'developer'};

const fulldetails = {...person, ...details};
console.log(fulldetails);

// {name: 'matin', age: 30}
登录后复制

15.延迟后重定向到新的url

setTimeout(() => location.href = https://www.linkedin.com/in/matin-imam/", 5000);
登录后复制

这 15 个 javascript 片段只是您通过几行代码可以实现的功能的一瞥​​。无论您是操作数组、字符串还是处理日期和时间,这些代码片段都可以帮助简化您的开发过程。


与我联系

如果您喜欢这篇文章并且想要联系,请随时在 linkedin 上与我联系。我很乐意联系并分享更多有关软件开发的见解!

在 linkedin 上与我联系

立即学习Java免费学习笔记(深入)”;

以上就是简短而有趣的 JavaScript 片段的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号