u.object = {}
/**
* @description 获取对象的属性集合
* @param {Object} obj 源对象
* @return {Array} 属性名的数组
*/
u.object.keys = function(obj) {
if (u.isEmpty(obj)) return []
var ret = []
try {
ret = Object.keys(obj)
}
catch (e) {
for (var key in obj) {
ret.push(key)
}
}
return ret
}
/**
*@description 获取对象属性的值 的集合
* @param {Object} obj 源对象
* @return {Array} 属性值的数组
*/
u.object.values = function(obj) {
if (u.isEmpty(obj)) return []
var ret = []
try {
ret = Object.values(obj)
}
catch (e) {
for (var key in obj) {
ret.push(obj[key])
}
}
return ret
}
/**
* @description 合并对象( Object.assign() 拷贝的是属性值,不属于深拷贝。深拷贝请参考 u.extend() )
* @param {Object} target 目标对象
* @param arguments 后面的属性会覆盖掉前面的
*/
u.object.assign = function(target) {
if (!u.isObject(target)) return
try {
Object.assign.apply(window, arguments)
}
catch (e) {
for (var i = 1; i < arguments.length; i++) {
var nextObj = arguments[i]
if (u.isObject(nextObj)) {
for (var nextKey in nextObj) {
target[nextKey] = nextObj[nextKey]
}
}
}
}
}
/**
* @description 选择对象中的一个(多个)属性
* @param {Object} obj 源对象
* @param {String Array} keys 属性名集合
* @return {Object} 新对象
*/
u.object.select = function(obj, keys) {
if (u.isEmpty(obj) || u.isEmpty(keys)) return {}
var ret = {}
if (!u.isArray(keys)) keys = [keys]
u.forEach(keys, function(i, key) {
ret[key] = obj[key]
})
return ret
}
/**
* @description 修改属性名
* @param {Object} obj 要修改的对象
* @param {String} oldKey 原来的属性名
* @param {String} newKey 新的属性名
* @param {Boolean} keepOld 是否保留旧的属性,默认为false
*/
u.object.rename = function(obj, oldKey, newKey, keepOld) {
if (u.isEmpty(obj)) return
if (obj[oldKey]) {
obj[newKey] = obj[oldKey]
if (!keepOld) u.object.remove(obj, oldKey)
}
}
/**
* @description 获取对象的属性值(支持多层数据)
* @param {Object} obj 对象
* @param {String} propertyName 属性名 'data.child.name'
* @param {Boolean} ignoreCase 忽略属性名大小写,默认false
*/
u.object.getValue = function(obj, propertyName, ignoreCase) {
var propertyValue = null
if (!obj) return propertyValue
if (u.isEmpty(propertyName)) return propertyValue
var pointIndex = propertyName.indexOf('.')
if (pointIndex > -1) {
obj = obj[propertyName.substring(0, pointIndex)]
return u.object.getValue(obj, propertyName.substring(pointIndex + 1), ignoreCase)
}
else {
u.forEach(obj, function(key, value) {
if (u.equal(key, propertyName, ignoreCase)) {
propertyValue = value
return false
}
})
}
return propertyValue
}
/**
* @description 序列化对象
* @param {Object} paramObj 源对象
* @return {String}
*/
u.object.serialize = function(paramObj) {
var name, value, fullSubName, subName, subValue, innerObj
var ret = ''
for (name in paramObj) {
value = paramObj[name]
if (value instanceof Array) {
for (var i = 0; i < value.length; ++i) {
subValue = value[i]
fullSubName = name + '[' + i + ']'
innerObj = {}
innerObj[fullSubName] = subValue
ret += u.object.serialize(innerObj) + '&'
}
}
else if (value instanceof Object) {
for (subName in value) {
subValue = value[subName]
fullSubName = name + '[' + subName + ']'
innerObj = {}
innerObj[fullSubName] = subValue
ret += u.object.serialize(innerObj) + '&'
}
}
else if (value !== undefined && value !== null) { ret += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&' }
}
ret = ret.substring(0, ret.length - 1)
return ret
}
/**
* @description 删除对象中指定的属性 或 值为空的属性(undefined, null, '')
* @param {Object} obj 源对象
* @param {String Array} keys 属性名集合,不传则删除为空的属性
* @return {Object}
*/
u.object.remove = function(obj, keys) {
if (u.isEmpty(obj)) return obj
var ret = {}
var es6 = true
if (!u.isEmpty(keys)) {
if (!u.isArray(keys)) keys = [keys]
u.forEach(keys, function(i, key) {
try {
delete obj[key]
}
catch (e) {
es6 = false
return false
}
})
if (es6) return obj
else {
u.forEach(obj, function(key, value) {
if (keys.indexOf(key) === -1) {
ret[key] = value
}
})
return ret
}
}
else {
u.forEach(obj, function(key, value) {
var wrongful = (value === null || value === undefined || value === '')
try {
if (wrongful) delete obj[key]
}
catch (e) {
es6 = false
if (!wrongful) {
ret[key] = value
}
}
})
if (es6) return obj
else return ret
}
}
/**
* @description 清空对象
* @param {Object} obj 源对象
* @param {Array} keys 属性名集合,不传则清空全部属性
* @return {Object} 清空后的对象
*/
u.object.clear = function(obj, keys) {
if (u.isEmpty(obj)) return {}
if (keys) {
if (!u.isArray(keys)) keys = [keys]
u.forEach(keys, function(i, key) {
obj[key] = ''
})
}
else {
for (var key in obj) {
obj[key] = ''
}
}
return obj
}
npm i sg-utils -S
https://github.com/shiguang0116/sg-utils
JavaScript工具类(一):util.js创建及上传
JavaScript工具类(二):cookie缓存
JavaScript工具类(三):localStorage本地储存
JavaScript工具类(四):数据类型
JavaScript工具类(五):string字符串
JavaScript工具类(六):number数字
JavaScript工具类(七):array数组
JavaScript工具类(八):object对象
JavaScript工具类(九):date日期
JavaScript工具类(十):base64编码、解码
JavaScript工具类(十一):浏览器、移动端类型
JavaScript工具类(十二):validate表单验证
JavaScript工具类(十三):url路径处理
JavaScript工具类(十四):json数据格式
JavaScript工具类:util.js用法实例