设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

开发中常用的 25 个JavaScript 单行代码

发布时间:2019-06-26 16:28 所属栏目:21 来源:alentan
导读:1.强制布尔值 要将变量强制转换为布尔值而不更改其值: constmyBoolean=!!myVariable; !!null//false !!undefined//false !!false//false !!ture//ture !!//false !!string//true !!0//false !!1//true !!{}//true !![]//true 2.基于某个条件为对象设置属

开发中常用的 25 个JavaScript 单行代码

1.强制布尔值

要将变量强制转换为布尔值而不更改其值:

  1. const myBoolean = !! myVariable; 
  2. !!null // false 
  3. !!undefined // false 
  4. !!false // false 
  5. !!ture // ture 
  6. !!"" // false 
  7. !!"string" // true 
  8. !!0 // false 
  9. !!1 // true 
  10. !!{} // true 
  11. !![] // true 

2.基于某个条件为对象设置属性

要使用spread运算符有条件地在对象上设置属性:

  1. const myObject = {... myProperty && {propName:myPoperty}}; 
  2. let myProperty = 'Jhon' 
  3. const myObject = {...myProperty && {propName: myProperty}}; // {propName: "Jhon"} 
  4. let myProperty = '' 
  5. const myObject = {...myProperty && {propName: myProperty}}; // {} 

如果myProperty结果为false,则 && 失败并且不设置新属性; 否则,如果不为空,&& 将设置新属性并覆盖原来的值。

3.合并对象

  1. const mergedObject = { ...objectOne, ...objectTwo }; 
  2. const mergedObject = { ...{name: 'Jhon', age: '18'}, ...{name1: 'jhon1', age1: '12'}}; 
  3. // {name: "Jhon", age: "18", name1: "jhon1", age1: "12"} 
  4. const mergedObject = { ...{name: 'Jhon', age: '18'}, ...{name: 'jhon1', age:'12'}}; 
  5. // {name: "jhon1", age: "12"} 

支持无限制合并,但如果对象之间存在相同属性,则后面属性会覆盖前面属性。*请注意,这仅适用于浅层合并。

4.交换变量

要在不使用中间变量的情况下交换两个变量的值:

  1. [varA,varB] = [varB,varA]; 
  2. let a = 1; 
  3. let b = 2; 
  4. [a, b] = [b, a] // a = 2 b = 1 

5.删除Boolean 为 false 值

  1. const clean = dirty.filter(Boolean); 
  2. const clean = [0, false, true, undefined, null, '', 12, 15].filter(Boolean); 
  3. // [true, 12, 15] 

这将删除值等于:null,undefined,false,0 和空字符串('')。

6.转换元素类型

要将Number元素转换为String元素:

  1. const stringArray = numberArray.map(String); 
  2. const stringArray = [1, 2, 3].map(String); 
  3. ["1", "2", "3"] 

如果数组包含字符串,字符串原样保留。 这也可以用于将String元素转换为Number类型:

  1. const numberArray = stringArray.map(Number); 
  2. const stringArray = ["1", "2", "3"].map(String); 
  3. // [1, 2, 3] 

7.格式化对象为JSON代码

要以可读的格式显示JSON代码:

  1. const formatted = JSON.stringify(myObj, null, 4); 
  2. const formatted = JSON.stringify({name: 'Jhon', age: 18, address: 'sz'}, null, 4); 
  3. /* 
  4.  "name": "Jhon", 
  5.  "age": 18, 
  6.  "address": "sz" 
  7. */ 

该字符串化命令有三个参数。第一个是Javascript对象。第二个是可选函数,可用于在JSON进行字符串化时对其执行操作。最后一个参数指示要添加多少空格作为缩进以格式化JSON。省略最后一个参数,JSON将返回一个长行。如果myObj中存在循环引用,则会格式失败。

8.快速创建数字数组

要创建一个数组并用数字填充它,索引为零:

  1. const numArray = Array.from(new Array(10), (x, i)=> i); 
  2. // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

9.随机生成六位数字验证码

  1. const code = Math.floor(Math.random() * 1000000).toString().padStart(6, "0"); 
  2. // 942377 

10.身份证正则

  1. const IDReg= /(^[1-9]d{5}(18|19|([23]d))d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)d{3}[0-9Xx]$)|(^[1-9]d{5}d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)d{2}[0-9Xx]$)/; 

11.window.location.search 转 JS 对象

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读