I have been facing a problem of which I am unable to call a .length() method to a data array in javascript. The purpose is to be able to iterate through an array of date time string in order to convert them to date objects in javascript. Here is my code.
My data:
HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"sales_time_axis": [
[
"2019-12-29T10:42:25Z"
],
[
"2019-12-23T03:13:03Z"
],
[
"2019-12-23T02:50:51Z"
]
],
The ajax call:
$.ajax({
type: "GET",
url: endpoint,
success: function(data) {
window.sales_time = data.sales_time_axis
},
error: function(error_data) {
console.log('error')
console.log(error_data)
}
})
The for loop:
var sales_time;
var date_array = []
for(i = 0 ; i < sales_time.length ; i++){
date_array.push(new Date(sales_time[i]))
}
console.log(data_array)
I have declared sales_time as a global variable , however I'm getting this error:
(index):179 Uncaught TypeError: Cannot read property 'length' of undefined
at (index):179
Your help will be greatly appreciated!