Зміст курсу
JavaScript Data Structures
JavaScript Data Structures
Challenge: Sorting and Extracting Properties
Task
Given an array of books, each represented by an object with properties (title
, author
, publicationYear
), create a solution that performs the following tasks:
- Sort the array of books:
- Sort by title in ascending order;
- Sort by author in descending order;
- Sort by year in descending order.
- Extract specific properties into separate arrays:
- Create an array containing only the titles of books (sorted by title in ascending order);
- Create an array containing only the authors of books (sorted by author in descending order);
- Create an array containing only the publication years of books (sorted by year in descending order).
Ensure that the original array of books remains unaltered.
const books = [ { title: "Noughts & Crosses", author: "Malorie Blackman", publicationYear: 2001, }, { title: "Priestdaddy", author: "Patricia Lockwood", publicationYear: 2017, }, { title: "The Cost of Living", author: "Deborah Levy", publicationYear: 2018, }, ]; // Sort by `title` in ascending order const sortedByTitleAscending = ___ .sort((a, b) => a.title.___(b.title)) .___((book) => book.title); // Sort by `author` in descending order const sortedByAuthorDescending = [...books] .___((a, b) => b.___.localeCompare(a.___)) .map((book) => book.author); // Sort by `year` in descending order const sortedByYearDescending = [...books] .sort((a, b) => ___ ___ ___) .___((book) => book.publicationYear); console.log("Sorted by Title (Ascending):", sortedByTitleAscending); console.log("Sorted by Author (Descending):", sortedByAuthorDescending); console.log("Sorted by Year (Descending):", sortedByYearDescending);
Expected output:
- For sorting by title, utilize
localeCompare()
with thetitle
property. - For sorting by author, apply
localeCompare()
with theauthor
property. - For sorting by year, use a numeric comparison based on the
publicationYear
property. - Utilize the
map()
method to create new arrays with specific properties. - Create a callback function for
map()
that returns the desired property for each book. - For extracting titles, authors, and years, the callback functions should return the
title
,author
, andpublicationYear
properties, respectively. - Ensure that the original array of books remains unaltered. Use the spread (
[...books]
) syntax to create a copy for sorting and extracting.
const books = [ { title: "Noughts & Crosses", author: "Malorie Blackman", publicationYear: 2001, }, { title: "Priestdaddy", author: "Patricia Lockwood", publicationYear: 2017, }, { title: "The Cost of Living", author: "Deborah Levy", publicationYear: 2018, }, ]; // Sort by `title` in ascending order const sortedByTitleAscending = [...books] .sort((a, b) => a.title.localeCompare(b.title)) .map((book) => book.title); // Sort by `author` in descending order const sortedByAuthorDescending = [...books] .sort((a, b) => b.author.localeCompare(a.author)) .map((book) => book.author); // Sort by `year` in descending order const sortedByYearDescending = [...books] .sort((a, b) => b.publicationYear - a.publicationYear) .map((book) => book.publicationYear); console.log("Sorted by Title (Ascending):", sortedByTitleAscending); console.log("Sorted by Author (Descending):", sortedByAuthorDescending); console.log("Sorted by Year (Descending):", sortedByYearDescending);
Дякуємо за ваш відгук!