I am encountering an issue where I am unable to utilize the .length() method on a data array in javascript. My goal is to iterate through an array of date time strings to convert them into date objects using javascript. Included below is my code.
Sample 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 defined sales_time as a global variable, yet I am receiving the following error:
(index):179 Uncaught TypeError: Cannot read property 'length' of undefined
at (index):179
Your assistance would be greatly valued!