/**
* Searches for content within date values in an array of objects.
* Primarily for development purposes.
* @param {Array<Object>} data An array of objects, each containing a 'date' property.
* @param {string} searchTerm The term to search for within the date values.
* @returns {Array<Object>} An array of objects containing the search term within the date.
*/
function searchDates(data, searchTerm) {
if (!Array.isArray(data)) {
console.error("Input data must be an array.");
return []; // Return empty array if input is invalid
}
if (typeof searchTerm !== 'string') {
console.error("Search term must be a string.");
return [];
}
const results = [];
for (const item of data) {
if (item && typeof item.date === 'string') { //Check if date exists and is a string
if (item.date.toLowerCase().includes(searchTerm.toLowerCase())) {
results.push(item); // Add the object to results if the search term is found
}
}
}
return results;
}
//Example Usage (for testing)
//const data = [
// { id: 1, date: "2023-10-26" },
// { id: 2, date: "Another date string" },
// { id: 3, date: "2024-01-15" },
// { id: 4, date: "This date has the word date" }
//];
//
//const searchTerm = "date";
//const foundItems = searchDates(data, searchTerm);
//console.log(foundItems);
Add your comment