fix: 取值

main
Lei OT 3 months ago
parent c81e856939
commit 7ad28510a6

@ -1,6 +1,6 @@
{ {
"name": "@haina/npm", "name": "@haina/npm",
"version": "0.1.1", "version": "0.1.2",
"main": "./build/cjs/index.js", "main": "./build/cjs/index.js",
"module": "./build/esm/index.js", "module": "./build/esm/index.js",
"exports": { "exports": {

@ -642,13 +642,18 @@ export const numberFormatter = (number) => {
}; };
/** /**
* @param {string[] | string} keyArr
* @example * @example
* const obj = { a: { b: 'c' } }; * const obj = { a: { b: 'c' } };
* const keyArr = ['a', 'b']; * const keyArr = ['a', 'b'];
* getNestedValue(obj, keyArr); // Returns: 'c' * getNestedValue(obj, keyArr); // Returns: 'c'
*/ */
export const getNestedValue = (obj, keyArr) => { export const getNestedValue = (obj, keyArr) => {
return keyArr.reduce((acc, curr) => { let _keyArr = structuredClone(keyArr);
if (!Array.isArray(_keyArr)) {
_keyArr = [_keyArr];
}
return _keyArr.reduce((acc, curr) => {
return acc && Object.prototype.hasOwnProperty.call(acc, curr) ? acc[curr] : undefined; return acc && Object.prototype.hasOwnProperty.call(acc, curr) ? acc[curr] : undefined;
// return acc && acc[curr]; // return acc && acc[curr];
}, obj); }, obj);

Loading…
Cancel
Save