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');