attr-accessor

This commit is contained in:
KoroLion 2021-05-28 17:10:52 +03:00
parent 2db3b98a85
commit e0d2ed3bd0

26
src/attr-accessor.js Normal file
View File

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