简单网站建设模板,昆明网站做的好的公司,上海百度推广官网,怎么建个私人网站目录
1.判断是否是json字符串
2.获取当前网址
3.将文本复制到剪贴板
4.获取一个月的天数
5.展平数组
6.要修改getRandomItem函数以返回数组中的随机两个元素#xff0c;可以尝试以下代码 1.判断是否是json字符串
const isJson str {try {JSON.parse(str);return …目录
1.判断是否是json字符串
2.获取当前网址
3.将文本复制到剪贴板
4.获取一个月的天数
5.展平数组
6.要修改getRandomItem函数以返回数组中的随机两个元素可以尝试以下代码 1.判断是否是json字符串
const isJson str {try {JSON.parse(str);return true;} catch (e) {return false;}
};
console.log(isJson({name:小明,address:苏州})); //true
console.log(isJson({name:小王,address:南京})); //false
2.获取当前网址
const currentURL () window.location.href;
currentURL()
3.将文本复制到剪贴板
const copyTextToClipboard async (text) {await navigator.clipboard.writeText(text)}// 请注意此函数是异步的因为 writeText 函数返回一个 Promise。// 但是如果您想支持 Internet Explorer 等旧版浏览器则必须采用以下方法此解决方案依赖于输入字段而不是之前的基于 Promise 的解决方案。// HTMLinput idinput typetext valueThis is the text that gets copiedbutton idcopyCopy the text/button// JavaScriptconst copy () {const copyText document.querySelector(#input)copyText.select()document.execCommand(copy)}document.querySelector(#copy).addEventListener(click, copy)
4.获取一个月的天数
const daysInMonth (month, year) new Date(year, month, 0).getDate()
daysInMonth(2, 2024)// 29
daysInMonth(12, 2022)// 31
5.展平数组
const flatten (arr) arr.reduce((a, b) (Array.isArray(b) ? [...a, ...flatten(b)] : [...a, b]), [])flatten([[1, 2], [3, 4], [5, 6]])// [1, 2, 3, 4, 5, 6]flatten([[some, text], and, [some, more]])// [some, text, and, some, more]但是还有一种更短的方法可以实现这一点。我们可以在数组上调用 flat 方法并获得相同的结果。但是此功能尚不完全支持。尤其是在老版本的几个浏览器中缺乏对这个功能的支持。[[1, 2], [3, 4], [5, 6]].flat()// [1, 2, 3, 4, 5, 6][[some, text], and, [some, more]].flat()// [some, text, and, some, more]
6.要修改getRandomItem函数以返回数组中的随机两个元素可以尝试以下代码
const getRandomItems (arr, count) {const shuffled arr.slice().sort(() 0.5 - Math.random());return shuffled.slice(0, count);}console.log(getRandomItems([1, 7, 9, 3, 6], 2)); // [ 7, 9 ]console.log(getRandomItems([{ name: Ted }, { name: Philip }, { name: Jack }], 2)); // [ { name: Jack }, { name: Philip } ]console.log(getRandomItems([{ name: Ted }, 1, Some text, 4, { name: Jack }], 2));