79681440

Date: 2025-06-27 05:30:22
Score: 0.5
Natty:
Report link
const latestOfEachDocumentType = (documents) => {
  const latestMap = {};

  documents.forEach(doc => {
    const existing = latestMap[doc.docType];

    if (!existing || new Date(doc.pubDate) > new Date(existing.pubDate)) {
      latestMap[doc.docType] = doc;
    }
  });

  return Object.values(latestMap);
};

const filterDocuments = ({ documents, documentTypes = [], months = [], languages = [] }) => {
  return documents.filter(doc => {
    const matchType = documentTypes.length === 0 || documentTypes.includes(doc.docType);
    const matchLang = languages.length === 0 || (Array.isArray(doc.language) 
                          ? doc.language.some(lang => languages.includes(lang))
                          : languages.includes(doc.language));
    const docMonth = doc.pubDate.slice(0, 7); // "YYYY-MM"
    const matchMonth = months.length === 0 || months.includes(docMonth);

    return matchType && matchLang && matchMonth;
  });
};
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Elice Palasara