网站首页 > 博客文章 正文
1. 删除数组尾部元素
一个简单的用来清空或则删除数组尾部元素的简单方法就是改变数组的length属性值。
const arr = [11, 22, 33, 44, 55, 66]; // truncanting arr.length = 3; console.log(arr); //=> [11, 22, 33] // clearing arr.length = 0; console.log(arr); //=> [] console.log(arr[2]); //=> undefined |
2.使用对象解构来模拟命名参数
如果你需要将一系列可选项作为参数传入函数,那么你也许倾向于使用了一个对象(Object)来定义配置(Config)。
doSomething({ foo: 'Hello', bar: 'Hey!', baz: 42 }); function doSomething(config) { const foo = config.foo !== undefined ? config.foo : 'Hi'; const bar = config.bar !== undefined ? config.bar : 'Yo!'; const baz = config.baz !== undefined ? config.baz : 13; // ... } |
这是一个陈旧、但是很有效的方法,它模拟了JavaScript中的命名参数。不过呢,在doSomething中处理config的方式略显繁琐。在ES2015中,你可以直接使用对象解构。
function doSomething({ foo = 'Hi', bar = 'Yo!', baz = 13 }) { // ... } |
如果你想让这个参数是可选的,也很简单。
function doSomething({ foo = 'Hi', bar = 'Yo!', baz = 13 } = {}) { // ... } |
3. 使用对象解构来处理数组
可以使用对象解构的语法来获取数组的元素:
const csvFileLine = '1997,John Doe,US,john@doe.com,New York'; const { 2: country, 4: state } = csvFileLine.split(','); |
4. 在switch语句中用范围值
可以使用下面的技巧来写满足范围值的switch语句:
function getWaterState(tempInCelsius) { let state; switch (true) { case (tempInCelsius <= 0): state = 'Solid'; break; case (tempInCelsius > 0 && tempInCelsius < 100): state = 'Liquid'; break; default: state = 'Gas'; } return state; } |
5. await多个async函数
在使用async/await的时候,可以使用Promise.all来await多个async函数。
await Promise.all([anAsyncCall(), thisIsAlsoAsync(), oneMore()]) |
6. 创建一个纯(pure)对象
你可以创建一个100%的纯对象,他不从Object中继承任何属性或则方法(比如,constructor,toString()等等)。
const pureObject = Object.create(null); console.log(pureObject); //=> {} console.log(pureObject.constructor); //=> undefined console.log(pureObject.toString); //=> undefined console.log(pureObject.hasOwnProperty); //=> undefined |
7. 格式化JSON代码
JSON.stringify不止可以将一个对象字符化,还可以格式化输出JSON对象。
const obj = { foo: { bar: [11, 22, 33, 44], baz: { bing: true, boom: 'Hello' } } }; // The third parameter is the number of spaces used to // beautify the JSON output. JSON.stringify(obj, null, 4); // =>"{ // => "foo": { // => "bar": [ // => 11, // => 22, // => 33, // => 44 // => ], // => "baz": { // => "bing": true, // => "boom": "Hello" // => } // => } // =>}" |
8. 从数组中移除重复元素
ES2015中,有了集合的语法。通过使用集合语法和Spread操作,可以很容易将重复的元素移除:
const removeDuplicateItems = arr => [...new Set(arr)]; removeDuplicateItems([42, 'foo', 42, 'foo', true, true]); //=> [42, "foo", true] |
9. 平铺多维数组
使用Spread操作,可以很容易去平铺嵌套多维数组:
const arr = [11, [22, 33], [44, 55], 66]; const flatArr = [].concat(...arr); //=> [11, 22, 33, 44, 55, 66] |
可惜,上面的方法仅仅适用于二维数组。不过,通过递归,我们可以平铺任意维度的嵌套数组。
unction flattenArray(arr) { const flattened = [].concat(...arr); return flattened.some(item => Array.isArray(item)) ? flattenArray(flattened) : flattened; } const arr = [11, [22, 33], [44, [55, 66, [77, [88]], 99]]]; const flatArr = flattenArray(arr); //=> [11, 22, 33, 44, 55, 66, 77, 88, 99] |
就这些啦!我希望这些小技巧可以帮你写出更加漂亮的JS代码!如果还不够,那么不妨用Fundebug做你的辅助!
猜你喜欢
- 2024-10-20 轻量级的React数据流及状态管理解决方案,支持SSR服务器端渲染
- 2024-10-20 5 分钟掌握 JavaScript 实用窍门(javascript官网)
- 2024-10-20 前端必备的20种基本React工具「干货」
- 2024-10-20 async await 并发(async await会阻塞吗)
- 2024-10-20 8 张图帮你一步步看清 async,await 和 promise 的执行顺序
- 2024-10-20 Atom源码阅读系列一(nasm源码阅读笔记)
- 2024-10-20 2019年JavaScript几个秘密窍门,你知道吗?
- 2024-10-20 小猿圈分享6个提JavaScript 小技巧(下)
- 2024-10-20 js遍历那些事儿(js遍历tr)
- 2024-10-20 Google 开源 zx,用 async/await 编写 shell 脚本
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)