I have been given a task that I can’t seem to wrap my head around to complete successfully and need some help with writing the code to complete the task. Any help getting this accomplished would be much appreciated.
The filterBooks function takes in a search string and a list of books and returns all of the books that match the search input. It should use the flattenObjectValuesIntoArray function to search all fields within a book object easily.
Books object and the flattenObjectValuesIntoArray function is as mentioned below. I need assistance with finding a way to return the books that match the search input into an array.
When I just run the filterBooks function trying to make it fit the given flattenObjectValuesIntoArray function I receive the response back:
"Given the filter ‘fantasy’ your filterBooks function returned an array with ‘7’ elements, we expected it to return ‘2’"
I have no idea how to get it to narrow the search down effectively to get ‘2’. Please help!
Books List:
const books = [
{
title: "The City We Became",
author: "N. K. Jemisin",
tags: ["fantasy", "fiction", "afrofutursim", "science fiction", "sci-fi"]
},
{
title: "The Catcher in the Rye",
author: "J. D. Salinger",
tags: ["fiction", "young adult", "YA", "realism", "coming of age", "classic"]
},
{
title: "The Hundred Thousand Kingdoms",
author: "N. K. Jemisin",
tags: ["fantasy", "fiction", "adventure", "series"]
},
{
title: "Sapiens: A Brief History of Humankind",
author: "Yuval Noah Harari",
tags: ["nonfiction", "history", "anthropology", "science", "sociology"]
},
{
title: "Behave: The Biology of Humans at Our Best and Worst",
author: "Robert M. Sapolsky",
tags: ["nonfiction", "anthropology", "science", "sociology", "biology"]
},
{
title: "The Parable of the Talents",
author: "Octavia Butler",
tags: ["fiction", "dystopian", "science fiction"]
},
{
title: "1984",
author: "George Orwell",
tags: ["fiction", "dystopian", "science fiction", "classics", "adult"]
},
{
title: "Remarkably Bright Creatures",
author: "Shelby Van Pelt",
tags: ["fiction", "mystery", "magical realism"]
},
{
title: "Crying in H Mart",
author: "Michelle Zauner",
tags: ["memoir", "nonfiction", "autobiography"]
},
{
title: "Wild: From Lost to Found on the Pacific Crest Trail",
author: "Cheryl Strayed",
tags: ["nonfiction", "memoir", "adventure", "travel"]
}
]
Original code to flatten object:
// Flatten object keys into an array so that we search the entire object by the input value
const flattenObjectValuesIntoArray = (arrOfObjs) => {
let flattenedObj;
const flattenedObjsArr = [];
for (let obj = 0; obj < arrOfObjs.length; obj++) {
const objValues = Object.values(arrOfObjs[obj]);
flattenedObj = [...objValues.flat()]
flattenedObjsArr.push(flattenedObj)
}
return flattenedObjsArr;
};
My code making it fit the filterBooks function:
// Filter books based on search input and render filtered books to the DOM
let filterBooks = (books) => {
let flattenedObj;
const flattenedObjsArr = [];
for (let obj = 0; obj < books.length; obj++) {
const objValues = Object.values(books[obj]);
flattenedObj = [...objValues.flat()]
flattenedObjsArr.push(flattenedObj)
}
return flattenedObjsArr;
};
2
Answers
Assuming that we are looking for exact text match in the book attributes you can complete the filter function as below.
The code receives the book object and filter string, the flattened function is called, and the filter string is searched in each flattened book array, if a match is found, the book (not flattened) gets pushed into a results array.
The full code would look like below
First, if the goal of the task is to use the
flattenObjectValuesIntoArray
method for ease of filtering by the search term, then you should be calling the method fromfilterBooks
, not copying the same code and altering it 😉.Second, the input for the
flatten ObjectValuesIntoArray
method call should be a single book at a time, not the full array of books, that way the output is only the fields (title, author, and all tags) of the specific book we are looking at and not all fields from every book. Otherwise we would have no way to know which book the flattened field belonged to.To do this we loop through each book, taking advantage of the
flattenObjectValuesIntoArray
method to return all the book’s fields into a single array that we can again loop through and compare for any matching strings: