js 常用总结
1.截取字符串
var a="/s/d"; console.log(a.substr(0,a.indexOf("/",1))) // 得到/s
2. //json数组 合并去重保留后面的
var songs = [
{name:"羽根",artist:"羽根"},
{name:"晴天",artist:"晴天1"},
{artist:"周杰伦",name:"周杰伦1"}
];
var arr = [
{name:"羽根",artist:"羽根2"},
{name:"晴天",artist:"晴天2"},
]
for (let i = 0; i < arr.length; i++) {
songs.push(arr[i])
}
function unique(songs){
let result = {};
let finalResult=[];
for(let i=0;i<songs.length;i++){
result[songs[i].name]=songs[i];
}
for(let item in result){
finalResult.push(result[item]);
}
return finalResult;
}
console.log(JSON.stringify(unique(songs))); //[{"name":"羽根","artist":"羽根2"},{"name":"晴天","artist":"晴天2"},{"artist":"周杰伦","name":"周杰伦1"}]
3. 正则 替换<括号
this.detailsForm.map(item => {
if(item.indexOf("<") != -1) {
let trunItem = item.replace(/\</g, '<');
this.lists.push(trunItem);
} else {
this.lists.push(item);
}
})
4. js 判断数据类型方法及 缺点 ( typeof,instanceof,constructor,Object.prototype.toString.call() )
1. typeof
console.log(typeof 2); // number
console.log(typeof true); // boolean
console.log(typeof 'str'); // string
console.log(typeof []); // object []数组的数据类型在 typeof 中被解释为 object
console.log(typeof function(){}); // function
console.log(typeof {}); // object
console.log(typeof undefined); // undefined
console.log(typeof null); // object null 的数据类型被 typeof 解释为 object
缺点: 无法判断 [] {} 和null
2. instanceof
console.log(2 instanceof Number); // false
console.log(true instanceof Boolean); // false
console.log('str' instanceof String); // false
console.log([] instanceof Array); // true
console.log(function(){} instanceof Function); // true
console.log({} instanceof Object); // true
console.log(undefined instanceof Undefined); // Undefined is not defined
console.log(null instanceof Null); // Null is not defined
console.log(new Number(2) instanceof Number); // true
console.log(new Boolean(true) instanceof Boolean); // true
console.log(new String('str') instanceof String); // true
缺点: instanceof 定义: 判断一个对象是否是数据类型的实例。 2, true ,'str'不是实例,所以判断值为false 无法判断null和undefined
3. constructor
console.log((2).constructor === Number); // true
console.log((true).constructor === Boolean); // true
console.log(('str').constructor === String); // true
console.log(([]).constructor === Array); // true
console.log((function() {}).constructor === Function); // true
console.log(({}).constructor === Object); // true
缺点: 如果原型被更改则类型也被更改 eg:
function Fn(){};
Fn.prototype=new Array();
var f=new Fn();
console.log(f.constructor===Fn); // false
console.log(f.constructor===Array); // true
4. Object.prototype.toString.call()
var a = Object.prototype.toString;
console.log(a.call(2)); "[object Number]"
console.log(a.call(true)); "[object Boolean]"
console.log(a.call('str')); "[object String]"
console.log(a.call([])); "[object Object]"
console.log(a.call(function(){})); "[object Function]"
console.log(a.call({})); "[object Array]"
console.log(a.call(undefined)); "[object Undefined]"
console.log(a.call(null)); "[object Null]"
完美
js 常用总结的更多相关文章
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- Js常用技巧
摘录:http://crasywind.blog.163.com/blog/static/7820316920091011643149/ js 常用技巧 1. on contextmenu=" ...
- JS常用的标准函数
原文:JS常用的标准函数 1.Array类型函数 array.concat(item...) 函数功能:关联数组,实现数组相加功能,但并不影响原先数组,concat返回新数组. array.join( ...
- JS 常用功能收集
JS 常用效果收集 1. 回到顶部>> 爱词霸
- JS常用校验方法(判断输入框是否为空,数字,电话,邮件,四舍五入等)
JS常用校验方法: 1.判断输入框是否为空,为空时弹出提示框 2.关闭窗口 3.检查输入字符串是否为数字 4.强制把大写转换成小写 5.手机号码校验,长度为11位数字. 6.电子邮件校验 7.电话号码 ...
- Node.js 常用工具
Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherit ...
- JS常用正则表达式备忘录
摘要: 玩转正则表达式. 原文:JS常用正则表达式备忘录 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 正则表达式或"regex"用于匹配字符串的各个部分 下面是 ...
- js常用数据类型(Number,String,undefined,boolean) 引用类型( function,object,null ),其他数据类型( 数组Array,时间Date,正则RegExp ),数组与对象的使用
js常用数据类型 数字类型 | 字符串类型 | 未定义类型 | 布尔类型 typeof()函数查看变量类型 数字类型 Number var a1 = 10; var a2 = 3.66; conso ...
- js常用身份校验规则
js常用身份校验规则 var Validator = { extractBirth: function(id) { // 身份证提取出生年月 var re = null, split, year, m ...
- JS常用类
JS常用类 一.Number 1.常用数字 整数:10 小数:3.14 科学计数法:1e5 | 1e-5 正负无穷:Infinity | -Infinity 2.常用进制 二进制:0b1010 八进制 ...
随机推荐
- select的disabled形式的数据,使用表单序列化方式无法将数据传到后台
之前博客里有讲述到使用表单序列化的方式传递数据到后台,那里是将数据为disabled形式的内容剔除掉了,所以为disabled的select肯定也是传不过去的. 解决方式: 1.在序列化表单方法之前将 ...
- IDEA中的JUNIT测试
安装插件 Ctrl+Alt+s→Plugins→junitgenerator v2.0 Alt+insert 选中JUnit test 中JUnit4 package test.com.demo.co ...
- (分块暴力)Time to Raid Cowavans CodeForces - 103D
题意 给你一段长度为n(1 ≤ n ≤ 3·1e5)的序列,m (1 ≤ p ≤ 3·1e5)个询问,每次询问a,a+b,a+2b+...<=n的和 思路 一开始一直想也想不到怎么分,去维护哪些 ...
- [bzoj3143] [洛谷P3232] [HNOI2013] 游走
Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点, ...
- 指定HTML标签属性 |Specifying HTML Attributes| 在视图中生成输出URL |高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼
结果呢: <a class="myCSSClass" href="/" id="myAnchorID">This is an o ...
- Quartz.Net和队列应用demo
using System; using System.Collections.Generic; using System.Threading; namespace ConsoleApplication ...
- C#服务端的GET、POST请求
一.HttpClient方式,程序集 System.Net.Http.dll GET: HttpClient httpClient = new HttpClient(); string result ...
- HTML-01-HTML格式
<!DOCTYPE html><!--告诉浏览器这个文档是html5版本的文档声明--> <html> <!--html是我们网页中最大的标签--> & ...
- 解决虚拟机中Linux不能上网的问题
第一步.虚拟机中的设置 点击右上角的网络标识,点击network settings,选择Wired,Add Profile,IPv4,Addresses设置成Automatic(DHCP),完成设置点 ...
- 数据结构与算法 --- js描述队列
js描述队列 队列的特性是只能在队尾插入元素,在队首删除元素,先进先出: 队列被用在很多地方,比如提交操作系统执行的一系列进程,打印任务池,模拟现实中的排队: //队列类 function Queue ...