[Vuejs]-How to get current month from json before making a calculation

0👍

You should start by getting the current month, since that’s what you’re going to use to filter your results.

Then, you should use the .filter function on the array to find the result you’re looking for.

I don’t really know what the forEach loop is for. Is there a possiblity of more than one result matching the current month?

created(){
    // Start by getting the current month.
    // N.B. - getMonth counts months starting at 0, i.e. January - month 0
    const currentMonth = new Date().getMonth()

    biService.getBIGraphStatsForCompany()
        .then((result) => {
            const data = result.data.propertyStatsPerMonth.find(row => new Date(row.date) === currentMonth;

            // use data to do your calculations
        }))
        .catch(error => console.error(error));
  }

Leave a comment