//longhandif(arr.indexOf(item) > -1) { // item found }if(arr.indexOf(item) === -1) { // item not found}//shorthandif(~arr.indexOf(item)) { // item found}if(!~arr.indexOf(item)) { // item not found}按位(?)运算符将返回非-1的真实值 。取反就像做!?一样简单 。另外,我们也可以使用include()函数:
if (arr.includes(item)) { // true if the item found}28. Object.entries()此功能有助于将对象转换为对象数组 。
const data = https://www.isolves.com/it/cxkf/yy/js/2021-01-11/{ test1: 'abc', test2: 'cde', test3: 'efg' };const arr = Object.entries(data);console.log(arr);/** Output:[ [ 'test1', 'abc' ],[ 'test2', 'cde' ],[ 'test3', 'efg' ]]**/29. Object.values()这也是ES8中引入的一项新功能,它执行与Object.entries()类似的功能,但没有关键部分:
const data = https://www.isolves.com/it/cxkf/yy/js/2021-01-11/{ test1: 'abc', test2: 'cde' };const arr = Object.values(data);console.log(arr);/** Output:[ 'abc', 'cde']**/30. Double Bitwise简写(双重NOT按位运算符方法仅适用于32位整数)
// LonghandMath.floor(1.9) === 1 // true// Shorthand~~1.9 === 1 // true31.重复一个字符串多次要一次又一次地重复相同的字符,我们可以使用for循环并将它们添加到同一循环中,但是如果我们有一个简写方法呢?
//longhand let test = ''; for(let i = 0; i < 5; i ++) {test += 'test '; } console.log(str); // test test test test test //shorthand 'test '.repeat(5);32.在数组中查找最大值和最小值const arr = [1, 2, 3]; Math.max(…arr); // 3Math.min(…arr); // 133.从字符串中获取字符let str = 'abc';//Longhand str.charAt(2); // c//Shorthand Note: If we know the index of the array then we can directly use index insted of character.If we are not sure about index it can throw undefinedstr[2]; // c34.功率速记数学指数幂函数的简写:
【2021年要了解的34种JavaScript优化技术】//longhandMath.pow(2,3); // 8//shorthand2**3 // 8
推荐阅读
- 不懂就要问讲述了什么的故事?不懂就要问是什么故事
- 高速网络新时代 路由器配置要注意
- 张清怎么被擒的 张横要如何杀宋江
- Mybatis 批量插入万条数据
- 安化黑茶煮好还是泡好,好普洱茶需要慢慢泡
- 杯中的茶垢要清洗,茶杯里的茶垢要清除吗
- 半天腰品茶要领,三点不可或缺的品茶茶艺要领
- 8瓦功率一天要多少电怎么算?8瓦功率一天要多少电钱
- fdm3d打印设备的后处理过程包括?简述fdm 3d打印机的主要构造_1
- 保管茶叶小妙方,茶叶贮藏保管的重要性
