JSFiddle - React, Tailwind, and code Playground

by Ryan J

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.1/axios.js"></script>
<script src="https://momentjs.com/downloads/moment.min.js"></script>

JavaScript

var format = 'YYYY-MM-DD',
  start = "2017-12-08",
  end = "2017-12-08",
  checkStart = "2017-12-08";

var startDate = moment(start).format(format);
var endDate = moment(end).format(format);

var bool1 = moment(checkStart).isBetween(moment(start).format(format), moment(end).format(format), null, '[)');
console.log(bool1)


var fundCode = 460
var selectedDate = moment('2017-10-31');

var arrHolder = [];
axios
  .get(`https://content.rbcgam.com/funds/growth-of-10k?rbcFundCode=${fundCode}`)
  .then(res => res.data)
  .then(data => {
    console.log(data.growthOf10K)
    var growthOf10kArrSinceInception = [];
    data.growthOf10K.map(fund => {
      if (moment(fund.key).isBefore(selectedDate) || moment(fund.key).isSame(selectedDate)) {
        growthOf10kArrSinceInception.push(fund);
      }
    });
    return growthOf10kArrSinceInception;
  })
  .then(growthOf10kArrSinceInception => {
    console.log("Show new array")
    console.log(growthOf10kArrSinceInception);

    //Get one year range and set in arr
    var tmpHolderArr = [];
    var oneYearDate = moment(selectedDate.subtract(1, 'year').add(1, 'month'));
    console.log("oneYearDate: " + oneYearDate.format('YYYY-MM-DD'));

    //Loop thru to get the ID of object in 1 Year
    var arrPointer = -1;
    var oneYearArrHolder = []
    growthOf10kArrSinceInception.map(fund => {
      var currentDateHolder = moment(fund.key);
      arrPointer++;
      if (oneYearDate.isSame(currentDateHolder)) {
        console.log(`Found date: ${fund.key} with the pointer value: ${arrPointer}`);
        console.log(`Showing the arr..`)
        console.log(growthOf10kArrSinceInception[arrPointer]);
        console.log(growthOf10kArrSinceInception[growthOf10kArrSinceInception.length - 1]);

        //Save new range of objs to one year arr holder
        oneYearArrHolder = growthOf10kArrSinceInception.slice(arrPointer, growthOf10kArrSinceInception.length);
      }
    });
    console.log("Showing oneYearArrHolder[]");
   ...