/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
packages/core-data/src/utils/get-nested-value.js
27 строк
1 KB
Matias Benedetto
Indicate nested paths on __experimentalSaveSpecifiedEntityEdits (#54161)
13 сен 2023, 04:13
Не верифицирован
13 сен 2023, 04:13
5179b9c
Код
Авторство
О чём код?
/** * Helper util to return a value from a certain path of the object. * Path is specified as either: * - a string of properties, separated by dots, for example: "x.y". * - an array of properties, for example `[ 'x', 'y' ]`. * You can also specify a default value in case the result is nullish. * * @param {Object} object Input object. * @param {string|Array} path Path to the object property. * @param {*} defaultValue Default value if the value at the specified path is undefined. * @return {*} Value of the object property at the specified path. */ export default function getNestedValue( object, path, defaultValue ) { if ( ! object || typeof object !== 'object' || ( typeof path !== 'string' && ! Array.isArray( path ) ) ) { return object; } const normalizedPath = Array.isArray( path ) ? path : path.split( '.' ); let value = object; normalizedPath.forEach( ( fieldName ) => { value = value?.[ fieldName ]; } ); return value !== undefined ? value : defaultValue; }