0

Not to sure why, but i am getting an error saying that the date is undefined. COuld someone help me out please?

let result = {};

let dates = [
"2020-10-20T10:28:20",
"2020-09-20T10:28:20",
"2020-08-20T10:28:20",
"2020-10-20T10:28:20",
"2020-09-20T10:28:20",
]


for (date of dates){
result[date.split('-')[1]] = 1 + (result[date.split('-')[1]] || 0);
}
console.log(result)

Updated code:

let dates = [
"2020-10-20T10:28:20",
"2020-09-20T10:28:20",
"2020-08-20T10:28:20",
"2020-10-20T10:28:20",
"2020-09-20T10:28:20",
]



dates.forEach(date=> {
  result[date.split('-')[1]] = 1 + (result[date.split('-')[1]] || 0);

})
console.log(result)
4
  • maybe for-of loop is an ES6 functionality and your env doesn't support that. Try using forEach instead like so: dates.forEach(date=>....rest of the logic here) Commented Oct 26, 2020 at 2:16
  • hey, thanks, could I also use a map? what would be the difference? could you also please show me how you would convert it? thanks! Commented Oct 26, 2020 at 2:17
  • map will give you back an array whereas forEach doesn't return anything. Commented Oct 26, 2020 at 2:19
  • its coming back with a date.split is not a function Commented Oct 26, 2020 at 2:21

1 Answer 1

1

Maybe try forEach like so:

dates.forEach(date=>{
  result[date.split('-')[1]] = 1 + (result[date.split('-')[1]] || 0);
});

let result = {};
let dates = [
"2020-10-20T10:28:20",
"2020-09-20T10:28:20",
"2020-08-20T10:28:20",
"2020-10-20T10:28:20",
"2020-09-20T10:28:20",
]



dates.forEach(date=> {
  result[date.split('-')[1]] = 1 + (result[date.split('-')[1]] || 0);

})
console.log(result)

Sign up to request clarification or add additional context in comments.

2 Comments

hey, thanks. getting a date.split is not a function. Any ideas?
I have added a code snippet here and it seems to be working. could you share what kind of environment are you using for testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.