From e0d2ed3bd0c3bb9cb75eb07aa7e6e4d5ad51f84b Mon Sep 17 00:00:00 2001 From: KoroLion Date: Fri, 28 May 2021 17:10:52 +0300 Subject: [PATCH] attr-accessor --- src/attr-accessor.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/attr-accessor.js diff --git a/src/attr-accessor.js b/src/attr-accessor.js new file mode 100644 index 0000000..e05f872 --- /dev/null +++ b/src/attr-accessor.js @@ -0,0 +1,26 @@ +/** + * Получение свойства объекта + * @param {object} src + * @param {string} path + */ +function get(src, path) { + path = path.split('.') + for (let key of path) { + src = src[key]; + if (src === undefined) { + return; + } + } + return src; +} + +var fixture = { + foo: { + bar: [ + {qux: 'bingo'}, + ], + }, +}; + +// Проверка +console.log(get(fixture, 'foo.bar.0.qux') === 'bingo');