mirror of
https://gitee.com/abc126655/finder-refferenct3
synced 2024-12-26 03:38:22 +00:00
67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
/**
|
|
* 对象工具
|
|
*/
|
|
export default class ObjectUtil {
|
|
|
|
/**
|
|
* 判断指定值是否是一个对象
|
|
* @param {any} arg 参数
|
|
*/
|
|
static isObject(arg) {
|
|
return Object.prototype.toString.call(arg) === '[object Object]';
|
|
}
|
|
|
|
/**
|
|
* 对象中是否包含指定的属性
|
|
* @param {object} object 对象
|
|
* @param {any} name 属性名
|
|
*/
|
|
static containsProperty(object, name) {
|
|
let result = false;
|
|
const search = (_object) => {
|
|
if (this.isObject(_object)) {
|
|
for (const key in _object) {
|
|
if (key == name) {
|
|
result = true;
|
|
return;
|
|
}
|
|
search(_object[key]);
|
|
}
|
|
} else if (Array.isArray(_object)) {
|
|
for (let i = 0; i < _object.length; i++) {
|
|
search(_object[i]);
|
|
}
|
|
}
|
|
}
|
|
search(object);
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 对象中是否包含指定的值
|
|
* @param {object} object 对象
|
|
* @param {any} value 值
|
|
*/
|
|
static containsValue(object, value) {
|
|
let result = false;
|
|
const search = (_object) => {
|
|
if (this.isObject(_object)) {
|
|
for (const key in _object) {
|
|
if (_object[key] === value) {
|
|
result = true;
|
|
return;
|
|
}
|
|
search(_object[key]);
|
|
}
|
|
} else if (Array.isArray(_object)) {
|
|
for (let i = 0; i < _object.length; i++) {
|
|
search(_object[i]);
|
|
}
|
|
}
|
|
}
|
|
search(object);
|
|
return result;
|
|
}
|
|
|
|
}
|