JS_HOME_WORK/41-find-single-post/solution.js
2025-02-05 08:47:22 +01:00

28 lines
937 B
JavaScript
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** ЗАДАЧА 41 - Поиск объектов в массиве
*
* 1. Создайте функцию "findPostById" с двумя параметрами:
* - ID поста
* - массив постов
*
* 2. Функция должна вернуть пост с определенным ID
*
* 3. Если поста с определенным ID в массиве постов нет,
* функция должна вернуть "undefined"
*
* 4. Также внутри функции выведите в консоль ID поста
*/
function findPostById(postId, posts) {
return posts.find((post) => post.postId === postId)
}
const posts = [
{ postId: 1355, commentsQuantity: 5 },
{ postId: 5131, commentsQuantity: 13 },
{ postId: 6134, commentsQuantity: 2 },
]
console.log(findPostById(6134, posts)) // { postId: 6134, commentsQuantity: 2 }
console.log(findPostById(4511, posts)) // undefined