-1

I am creating an Array of days between 2 given dates, the keys should be formatted as DD/MM/YYYY and the values should be numbers (prices set for each date)

It seems to work because the Array contains the values I give it (via a date picker) but I can not loop through this Array, probably because it's length returns 0 even though it contains elements

Here is a screenshot of the console log statement

enter image description here

Here is the code that creates the Array

  var arrayOfDatesBetween = new Array();
  // daysBetween = integer representing the count of days between the chosen dates
  for (let i = 0; i < daysBetween; i++) {
    // just add one day on each iteration but keep count of the first
    let q = i === 0 ? i : 1;
    let _date = _dateIn.setDate(_dateIn.getDate()+q);
    // lcsgDate() formats the date as I need it: DD/MM/YYYY
    let __date = lcsgDate(_date);
    // getDatePrice() gets the price for the given date by searching into another Array of date:price
    arrayOfDatesBetween[__date] = getDatePrice(__date);
  }
  // result
  console.log(arrayOfDatesBetween);
Nicola
  • 364
  • 3
  • 11

2 Answers2

1

I confirm that changing arrayOfDatesBetween from Array to Object solved the issue and I can now have non-integers as keys, just as I needed, Thanks for commenting and pointing me to the right direction

Nicola
  • 364
  • 3
  • 11
  • Please don't add comments as answers. Post comments as comments and additional information as edits to the question. – RobG Jul 02 '18 at 11:02
  • This is intentionally an answer because it solved the issue and I am waiting the 2 days imposed by stackoverflow before I can mark it as accepted. – Nicola Jul 02 '18 at 14:14
1

let arr = [1,2,3]
arr['someCustomDate'] = 'someCustomData'


console.log(arr) // [1,2,3]
console.log(arr['someCustomDate'])

You code is essentially same as above, you're defining the property of an array instead of pushing them into the array.

To handle in your situation, you have two options:

1: for every element of your array, create an object and push them into your array like below:

 var arrayOfDatesBetween = new Array();
 // daysBetween = integer representing the count of days between the chosen dates
 for (let i = 0; i < daysBetween; i++) {
 // just add one day on each iteration but keep count of the first
   let q = i === 0 ? i : 1;
   let _date = _dateIn.setDate(_dateIn.getDate()+q);
   // lcsgDate() formats the date as I need it: DD/MM/YYYY
   let __date = lcsgDate(_date);
   // getDatePrice() gets the price for the given date by searching into another Array of date:price

   //HERE <=======
   let newObjectElement = { date: __date, price: getDatePrice(__date)};
   //arrayOfDatesBetween[__date] = getDatePrice(__date);
   arrayOfDatesBetween.push(newObjectElement);
}
// result
console.log(arrayOfDatesBetween);

2: Remain your code, but using Object.keys to loop over __date.

Highly recommended to pick option 1 because thats the sole reason to use Array instead of pushing element as a key

Isaac
  • 12,042
  • 16
  • 52
  • 116