Skip to content

Instantly share code, notes, and snippets.

@oleggrishechkin
Created May 27, 2021 16:09
Show Gist options
  • Save oleggrishechkin/e6540881c459ecd9c14ef5a84ba2d987 to your computer and use it in GitHub Desktop.
Save oleggrishechkin/e6540881c459ecd9c14ef5a84ba2d987 to your computer and use it in GitHub Desktop.

шаблон

// написать фунцию, которая возвращает значение св-ва объекта по заданному пути

const getProp = (object, path) => {
  // код
};

const obj = {
  b: {
    c: 1
  }
}

console.log(getProp(obj, 'b.c'))) // 1

дополнительно

что будет если в объекте нет св-в которые содержит путь?

решение

const getProp = (object, path) => path.split('.').reduce((result, pathPart) => result?.[pathPart], object);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment