Reputation: 71
var eachTime = "11.00 am,11.15 am,11.30 am,11.45 am,11.45 am,12.00 pm,12:00 pm,12.15 pm,12:15 pm,12.30 pm";
How to make an array whose value is ['11.00 am','11.15 am'],['11.30 am','11.45 am'], ...,['12:15 pm','12.30 pm']
I need to make such array to pass to timepicker
for disabling this time.
Any one please help me
Upvotes: 0
Views: 1479
Reputation: 92854
Though there's a doubled items in your string, I can suggest the solution for converting the string into array of consecutive "time" pairs(used functions: String.split
, Array.forEach
and Array.push
):
var eachTime = "11.00 am,11.15 am,11.30 am,11.45 am,11.45 am,12.00 pm,12:00 pm,12.15 pm,12:15 pm,12.30 pm",
time_arr = eachTime.split(","), new_arr = [];
time_arr.forEach(function(v, k , arr){
if (k % 2 & 1) new_arr.push([arr[k-1], arr[k]]); // check for odd keys
})
console.log(JSON.stringify(new_arr, 0, 4));
The output:
[
[
"11.00 am",
"11.15 am"
],
[
"11.30 am",
"11.45 am"
],
[
"11.45 am",
"12.00 pm"
],
[
"12:00 pm",
"12.15 pm"
],
[
"12:15 pm",
"12.30 pm"
]
]
Upvotes: 1