0
\$\begingroup\$

I'm just wondering Is there any ways I could simplify the following code without making it too difficult:

Actually I want to extract specific year data from content node based on lastNumberOfYears variable.

Example: Content array has 2019,2018,2017,2016 years data and lastNumberOfYears value is 2 then output should have only 2019, 2018 data. other scenario is if content array is like 2020, 2014, 2013 then output should have 2020, 2014 data (if lastNumberOfYears value is 2)

My solution

Step 1 : Define predefined data

const content = [{
    "document": "/content/path/document1.pdf",
    "date": "2020-02-20"
  }, {
    "document": "/content/path/file.docx",
    "date": "2019-02-20"
  }, {
    "document": "/content/abc/document2.docx",
    "date": "2018-06-20"
  }, {
    "document": "/document/order/order.pdf",
    "date": "2020-06-20"
  }, {
    "document": "/content/order/update.pdf",
    "date": "2018-03-21"
  }, {
    "document": "/content/author/author.pdf",
    "date": "2017-03-21"
  }, {
    "document": "/content/path/dummy.pdf",
    "date": "2016-02-15"
  }, {
    "document": "/content/dummy/dummy.docx",
    "date": "2015-12-15"
}];

let lastNumberOfYears = 2;
let selectedYearArray = [];

Step 2: Convert content data into year base data

const result = content.reduce((current, value) => {
  const dateYear = new Date(value.date).getFullYear();
  current[dateYear] = current[dateYear] || [];
  current[dateYear].push(value);
  return current;
}, {});

/*
output is like
{
   "2015":[{...}],"2016":[{}],..."2020":[{}, {}]
}
*/

Step 3: Put specific year data in same order for result array

Object.values(result).forEach(value => value.reverse());

Step 4: Create Year array from object key

const yearsArray = Object.keys(result).sort((first, second) => (first > second ? -1 : 1));

Step 5: Create last number of year array using lastNumberOfYears variable Step 6: Extract Step 5 data from result array define in step 3 Step 7: Remove year key and join all array value of Step 6 outcome

if (yearsArray.length !== 0 && lastNumberOfYears <= yearsArray.length) {
  for (let i = 0; i < lastNumberOfYears; i++) {
    selectedYearArray.push(yearsArray[i]);
  }

  /*
    Step 5: console.log(selectedYearArray);
  */

  const filteredResult = Object.keys(result)
    .filter(key => selectedYearArray.includes(key))
    .reduce((obj, key) => {
      obj[key] = result[key];
      return obj;
    }, {});

  /*
    Step 6: console.log(filteredResult);
  */

  contentObj = [].concat.apply([],Object.values(filteredResult));

  /*
    Step 7:
  */
  console.log(contentObj.reverse());
}

Output:

[{
    "document": "/content/path/document1.pdf",
    "date": "2020-02-20"
}, {
    "document": "/document/order/order.pdf",
    "date": "2020-06-20"
}, {
    "document": "/content/path/file.docx",
    "date": "2019-02-20"
}]
New contributor
Gourav Makhija is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

Your Answer

Gourav Makhija is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.