Reputation: 123
My array:
[
{
"date":"2018-04-01",
"time":[{"10:00":"12"},{"12:00":"25"}]
},
{
"date":"2018-04-02",
"time":[{"10:00":"12"},{"12:00":"25"}]
},
{
"date":"2018-04-03",
"time":[{"10:00":"12"},{"12:00":"25"}]
}
]
I need to get every date and time. To get this I am using a for loop. But not able to get date and time.
My script:
var slots = req.body.availableSlots;
var count = slots.length;
for(var i=0;i<count;i++){
console.log(slots[i]);
console.log(slots[i].date);
}
When getting date
always says undefined
.
Upvotes: 1
Views: 70
Reputation: 72289
It seems like req.body.availableSlots
is coming as a multidimensional object array.
So full code need to be:-
var slots = req.body.availableSlots;
for(var i=0;i<count;i++){
var sub_array = slots[i];
for(j = 0; j<sub_array.length;j++){
console.log(sub_array[j].date);
}
}
Upvotes: 1
Reputation: 480
Instead of using jquery library (jQuery.parseJSON()) use javascript inbuilt JSON.parse
var slots = '[{"date":"2018-04-01","time":[{"10:00":"12"},{"12:00":"25"}]},{"date":"2018-04-02","time":[{"10:00":"12"},{"12:00":"25"}]},{"date":"2018-04-03","time":[{"10:00":"12"},{"12:00":"25"}]}]';
slots = JSON.parse(slots);
var count = slots.length;
for(var i=0;i<count;i++){
console.log(slots[i].date);
Upvotes: 0