Skip to content

Commit

Permalink
Упрощает получение значение свойства article.data.cover.mobile (#1291)
Browse files Browse the repository at this point in the history
Задача: получить поле(строку) `article.data.cover.mobile`
Необходимо:
- убедиться что `article.data.cover` существует и является объектом
  - получить значение поля `mobile` и вернуть его
- иначе, вернуть `undefined`

Для этого можем использовать оператор `?.`

`article.data.cover?.mobile` - вернёт `undefined` если:
- `cover` не содержит `mobile`
- `cover` не является объектом
  • Loading branch information
vitya-ne committed Aug 29, 2024
1 parent 04b9db6 commit d140702
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/scripts/modules/transform-article-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function transformArticleData(article) {
title: article.data.title,
cover: article.data.cover ?? {},
get imageLink() {
return Object.keys(this.cover).includes('mobile') ? `${this.cover.mobile}` : undefined
return this.cover?.mobile
},
description: article.data.description,
link: `/${section}/${article.fileSlug}/`,
Expand Down

0 comments on commit d140702

Please sign in to comment.