总结几个JS开发小技巧(转布尔,转字符数字,去除空格等等)

1 转换布尔值

除了常规的布尔值truefalse之外,JavaScript还将所有其他值视为 ‘truthy’ 或**‘falsy’**。

除非另有定义,否则 JavaScript 中的所有值都是'truthy',除了0“”nullundefinedNaN,当然还有false,这些都是**'falsy'**

我们可以通过使用负算运算符轻松地在truefalse之间切换。它也会将类型转换为“boolean”。

const isTrue  = !0;
const isFalse = !1;
const alsoFalse = !!0;
console.log(isTrue); // Result: true
console.log(typeof true); // Result: "boolean"

2 转换数字

使用加法运算符+可以快速实现相反的效果。

let int = "15";
int = +int;
console.log(int); // Result: 15
console.log(typeof int); Result: "number"

这也可以用于将布尔值转换为数字,如下所示

console.log(+true);  // Return: 1
console.log(+false); // Return: 0

在某些上下文中,+将被解释为连接操作符,而不是加法操作符。当这种情况发生时(你希望返回一个整数,而不是浮点数),您可以使用两个波浪号:~~

连续使用两个波浪有效地否定了操作,因为— ( — n — 1) — 1 = n + 1 — 1 = n。换句话说,~—16 等于15

const int = ~~"15"
console.log(int); // Result: 15
console.log(typeof int); Result: "number"

虽然我想不出很多用例,但是按位NOT运算符也可以用在布尔值上:~true = \-2~false = \-1

3转换字符串

要快速地将数字转换为字符串,我们可以使用连接运算符+后跟一组空引号""

const val = 1 + "";
console.log(val); // Result: "1"
console.log(typeof val); // Result: "string"

4浮点数转整数

如果希望将浮点数转换为整数,可以使用Math.floor()Math.ceil()Math.round()。但是还有一种更快的方法可以使用|(位或运算符)将浮点数截断为整数。

console.log(23.9 | 0);  // Result: 23
console.log(-23.9 | 0); // Result: -23

|的行为取决于处理的是正数还是负数,所以最好只在确定的情况下使用这个快捷方式。

如果n为正,则n | 0有效地向下舍入。如果n为负数,则有效地向上舍入。更准确地说,此操作将删除小数点后面的任何内容,将浮点数截断为整数。

你可以使用~~来获得相同的舍入效果,如上所述,实际上任何位操作符都会强制浮点数为整数。这些特殊操作之所以有效,是因为一旦强制为整数,值就保持不变。

删除最后一个数字

按位或运算符还可以用于从整数的末尾删除任意数量的数字。这意味着我们不需要使用这样的代码来在类型之间进行转换。

let str = "1553";
Number(str.substring(0, str.length - 1));

相反,按位或运算符可以这样写:

console.log(1553 / 10   | 0)  // Result: 155
console.log(1553 / 100  | 0)  // Result: 15
console.log(1553 / 1000 | 0)  // Result: 1

6取数组最后一项

数组方法slice()可以接受负整数,如果提供它,它将接受数组末尾的值,而不是数组开头的值。

let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(array.slice(-1)); // Result: [9]
console.log(array.slice(-2)); // Result: [8, 9]
console.log(array.slice(-3)); // Result: [7, 8, 9]

7es6数组去重

Set对象类型是在ES6中引入的,配合展开操作...一起,我们可以使用它来创建一个新数组,该数组只有唯一的值。

const array = [1, 1, 2, 3, 5, 5, 1]
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // Result: [1, 2, 3, 5]

此技巧适用于包含基本类型的数组:undefinednullbooleanstringnumber。(如果你有一个包含对象,函数或其他数组的数组,你需要一个不同的方法!)

8:字符串去除空格

1.使用String原型的trim()方法去除两端空格

trim() 方法会删除一个字符串两端的空白字符。在这个字符串里的空格包括所有的空格字符 (space, tab, no-break space 等)以及所有的行结束符(如 LF,CR)。

let str = ' hello world ';
str.trim()
//"hello world"

trim()方法有个问题,不兼容低版本浏览器,如果出现这种情况, 使用下面写法:

function myTrim(str) {
    if(String.prototype.trim) {
        return str.trim();
    }
    return str.replace(/^\s+(.*?)\s+$/g, "$1");
}
myTrim(' hello world ');
//"hello world"

可以看到, trim()方法只能删除字符串两端的空白, 那么中间也要删除怎么做?

2.使用split()和join()方法删除所有空格

//如果在字符串中仅存在空格,没有制表符等空白符,那么可以使用split()和join()方法来去空白:
 
let str = "   hello  world   !";
let result = str.split(" ").join("");
console.log(result);  //"helloworld!"
 

3.使用正则去除所有空格

而如果存在了制表符等空白符,上面的方法便无法去除:

let str = "\t hello  world  !";
let result = str.split(" ").join("");
console.log(result);  //"    helloworld!"

 可以使用正则表达式

//去除所有空格
let str = "\t hello  world  !";
let result = str.replace(/\s+/g, "");
console.log(result);  //"helloworld!"
//去除两头空格:
str = str.replace(/^\s+|\s+$/g,"");
//去除左空格:
str = str.replace( /^\s/, '');
//去除右空格:
str = str.replace(/(\s$)/g, "");

9. 对象判空

js判断一个object对象是否存在

if (JSON.stringify(data) === '{}') {
    return false // 如果为空,返回false,数组可以用同样的判断方式
}
if (Object.keys(object).length === 0) {
    return false // 如果为空,返回false,Object.keys(object)会返回一个空数组[]
}

10.正则匹配规则

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值