-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetValue.js
executable file
·37 lines (35 loc) · 1.13 KB
/
getValue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { getDataType } from './index';
/**
* @description 根据路径获取对象的值(也可用插件@babel/plugin-proposal-optional-chaining)
* @param {object} obj - 对象
* @param {string} keypath - 路径
* @param {any} defaultValue - 默认值
*/
export const getValue = (obj, keypath, defaultValue = undefined) => {
if (!obj) {
throw new Error('the first param obj is required');
}
if (!keypath) {
throw new Error('the second param keypath is required');
}
if (getDataType(obj) !== 'Object') {
throw new Error('the first param obj must be object');
}
if (getDataType(keypath) !== 'String') {
throw new Error('the second param keypath must be string');
}
// return String.prototype.split.call(keypath, /[,[\].]+?/)
// .filter(Boolean)
// .reduce((a, c) => (Object.hasOwnProperty.call(a,c) ? a[c] : defaultValue), obj)
keypath = keypath
.replace(/\[(\d+)\]/g, '.$1')
.split('.')
.filter((item) => !['', null, undefined].includes(item));
for (const key of keypath) {
obj = Object(obj)[key];
if (obj === undefined) {
return defaultValue;
}
}
return obj;
};