有用的javaScript技巧

2019-08-01  本文已影响0人  Mr丶何

数组去重

var j = [...new Set([1,2,2,4])];     // [1,2,4]

判断对象和数组方法

Object.prototype.toString.call({}) 返回字符串的 '[object Object]'

Object.prototype.toString.call([]) 返回字符串的 '[object Array]'

对象是否包含某个属性

Obj.hasOwnProperty('name')

过滤空值

let res = [1,2,0,undefined,null,false,''].filter(Boolean);   >>1,2

合并对象

const person = { a:1 };

const tools = { b: 2 };

const attributes = { c:3 };

const summary = {...person, ...tools, ...attributes};   >> {a:1, b:2, c:3}

上一篇 下一篇

猜你喜欢

热点阅读