首页 > 其他分享 >js各种方法

js各种方法

时间:2022-09-28 09:12:51浏览次数:47  
标签:isArray 各种 console name str1 js str Array 方法

判断类型、判断数据是否有值

typeof

let obj = {}
typeof obj === Object
// 根据typeof判断对象也不太准确
表达式                       返回值

typeof undefined           'undefined'

typeof null                'object'

typeof true                'boolean'

typeof 123                 'number'

typeof "abc"               'string'

typeof function() {}       'function'

typeof {}                  'object'

typeof []                  'object'

 

判断是否为数组 Array.isArray(obj)


// 下面的函数调用都返回 true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
Array.isArray(new Array('a', 'b', 'c', 'd'))
// 鲜为人知的事实:其实 Array.prototype 也是一个数组。
Array.isArray(Array.prototype); 
 
// 下面的函数调用都返回 false
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray('Array');
Array.isArray(true);
Array.isArray(false);
Array.isArray(new Uint8Array(32))
Array.isArray({ __proto__: Array.prototype });

 

判断对象里面是否包含某个字段

//用法:Reflect.has(obj, propName)
Reflect.has({name:"搞前端的半夏"}, "name"); // true
Reflect.has({name:"搞前端的半夏"}, "age"); // false
Reflect.has({name:"搞前端的半夏"}, "toString"); //true


//用法:Object.hasOwn
Object.hasOwn({name:"搞前端的半夏"}, "name"); // true
Object.hasOwn({name:"搞前端的半夏"}, "age"); // false
Object.hasOwn({name:"搞前端的半夏"}, "toString"); //true

 

字符串拦截替换

1、拦截字符串指定位置

var str = 'abcd9999';
var newStr = str.slice(2);
console.log(newStr); // 输出 cd9999;
newStr = str.slice(-2);
console.log(newStr); // 输出 99;
newStr = str.slice(2,4);
console.log(newStr); // 输出 cd;
newStr = str.slice(2,-2);
console.log(newStr); // 输出 cd99;

 

2、拦截字符串替换

var sei='1231231;252135135'
sei.replace(";"," ")
console.log(sei) //=>1231231252135135

 

3、拦截字符串替换

let str = 'aaabbbcccddd'
str = str.match(/aaa(\S*)ddd/)[1]
console.log(str)  // bbbccc


let str1 = 'name="xiaoming",age="18"'
str1 = str1.match(/"(\S*)"/)[1]
console.log(str1) // 得到的是  xiaoming

 

4、截取指定两个字符之间的内容

let str = 'aaabbbcccddd'
str = str.match(/aaa(\S*)ddd/)[1]
console.log(str)  // bbbccc


let str1 = 'name="xiaoming",age="18"'
str1 = str1.match(/"(\S*)"/)[1]
console.log(str1) // 得到的是  xiaoming

 

5、截取指定字符串之前的内容:

let str1 = 'name="xiaoming",age="18"'
str1 = str1.match(/=(\S*)/)[1]
console.log(str1) // 得到的是  name

 

6、截取指定字符串之后的内容:

let str1 = 'name="xiaoming",age="18"'
str1 = str1.match(/name=(\S*)/)[1]
console.log(str1) // 得到的是  "xiaoming",age="18"

 

标签:isArray,各种,console,name,str1,js,str,Array,方法
From: https://www.cnblogs.com/yuluochengxu/p/16736793.html

相关文章

  • 二叉树的前中后序遍历的两种方法
    前中后序遍历的记忆方式:前中后可以记为中间节点的顺序位置,如:前序遍历:中左右;中序遍历:左中右;后续遍历:左右中。//前序遍历:算法实现:前序遍历顺序为中左右。需要传......
  • JSON 语法
    JSON语法规则JSON语法是JavaScript对象表示语法的子集。数据在名称/值对中数据由逗号 , 分隔使用斜杆 \ 来转义字符大括号 {} 保存对象中括号 [] 保存......
  • js中的apply方法并模拟实现自己的apply方法
    apply方法定义call()方法,在mdn中的定义:apply()方法调用一个具有给定this值的函数,以及以一个数组(或类数组对象)的形式提供的参数。(它的作用和使用和call方法一致,唯一的......
  • js中的bind方法并模拟实现自己的bind方法
    Js中bind方法使用和实现前面我们已经模拟实现了call和apply方法,今天来实现下同样可以改变this指向但是又有点不同得方法-->bind方法。定义首先来看下bind方法在mdn中......
  • python安装weditor报错error subprocess-exited-with-error的解决方法
    使用pip安装weditor失败,报错内容:  解决方法:第一步输入gitclonehttps://github.com/openatx/weditor第二步输入 pip3install-eweditor     验......
  • 前端三件套 HTML+CSS+JS基础知识内容笔记
    HTML基础目录HTML基础HTML5标签doctype标签html标签head标签meta标签title标签body标签文本和超链接标签标题标签段落标签换行标签水平标签强调标签图片标签与超链接标签......
  • TE对象message js脚本简单写法
        TE里的对象和图层都支持添加message,其中message类型中有一类为Script,在弹出的框里可以写JavaScript脚本,下图就是切换到脚本信息下: 写一个了简单的脚本示例:<scrip......
  • JS 刷新页面所有方法
    window.location.reload();使用window.open()弹出的弹出窗口,刷新父窗口非模态刷新父页面:window.opener.location.reload()使用window.showDialog弹出的模式窗口模态刷新父......
  • VueJs 自定义过滤器使用总结
    在这个教程中,我们将会通过几个例子,了解和学习VueJs的过滤器。我们参考了一些比较完善的过滤器,比如orderBy和filterBy。而且我们可以链式调用过滤器,一个接一个过滤。因此,我......
  • vuejs 开发问题解决方案总结一
    文章中提到的很多东西都在我的demo中用到,我的demo地址:​​https://github.com/MrZhang123/Vue_project/tree/master/vue_spa_demo​​1.Vuejs组件vuejs构建组件使用Vue.comp......