0

I'm completely new to Javascript and building a booking calendar for car rental page and not sure how to get specific values out of object. I googled Javascript Object desctructuring, but only found out how to console.log all of the keys.

I have following code which outputs objects values (pic related)

$(this).datepicker('setUTCDates', newDates);
console.log(newDates)

enter image description here

, but I'm trying to get only the date, which I thought would be done with

$(this).datepicker('setUTCDates', newDates);
console.log(newDates.getDay())

However, I'm getting getDay() function doesn't exist.

I also tried putting this into Map:

$(this).datepicker('setUTCDates', newDates);
const obj = newDates;
const map = new Map(Object.entries(obj));
console.log(map); 

, but getting following output:

enter image description here

Now I'm not sure how to scrape out only value from given key. I'm also not sure what I'm looking at when I open the console and skimming through object, if anyone can point me out to read upon that I'd be thankful.

  • can you please post your first code output, hope that will make sense to me – Md. Jamal Uddin Nov 20 '21 at 21:01
  • 1
    It's the whole object and its values - [Mon Nov 01 2021 01:00:00 GMT+0100 (Central European Standard Time)] as posted in picture. – user14584183 Nov 20 '21 at 21:08
  • Please [edit] your post to show the actual data, rather than a screenshot. You can use `console.log(JSON.stringify(newDates, null, 2))` to get a nicely formatted output. – Heretic Monkey Nov 20 '21 at 21:10
  • Did you try newDates[0].getDay() ? – Thallius Nov 20 '21 at 21:10
  • Note that you likely have an array, not an object, in `newDates`. `getDay()` will return the day of the week as a number, with Sunday as 0 and Saturday as 6. – Heretic Monkey Nov 20 '21 at 21:11
  • @Thallius, now I'm getting the right value, whichever day I'm clicking. However, when I for example click Day 1 and then Day 2, I'm only getting value 1 only, instead of 1 and 2. – user14584183 Nov 20 '21 at 21:13

1 Answers1

1

try this:

$(this).datepicker('setUTCDates', newDates);
newDates.map(date => console.log(date.getDay());

newDates is an array. so, first, you have to loop through the array and console each item or item property or method. that will work for sure.

Md. Jamal Uddin
  • 754
  • 8
  • 17