我正在调用API获取以下json数据
[{"id":"1","start_time":"07:30 AM","end_time":"11:00 AM","status":"1"},{"id":"2","start_time":"11:00 AM","end_time":"02:00 PM","status":"1"},{"id":"3","start_time":"02:00 PM","end_time":"05:00 PM","status":"1"},{"id":"4","start_time":"05:00 PM","end_time":"08:30 AM","status":"1"},{"id":"5","start_time":"09:00 PM","end_time":"11:00 PM","status":"1"},{"id":"6","start_time":"11:00 PM","end_time":"01:30 AM","status":"1"},{"id":"7","start_time":"01:30 AM","end_time":"07:30 AM","status":"1"}]
start_time = 07:30 AM ,没关系
但是当我 start_time = 11:00 AM(零分钟)时,我想将其显示为 start_time = 11 AM 。
I have marked the zero minutes here in output
如果分钟大于零,则显示它们 否则用冒号(:)标志隐藏它们 我该怎么做?
答案 0 :(得分:2)
function reduceTime(obj, prop) {
var colon_index = obj.indexOf(':');
if(obj[prop][colon_index + 1] == '0' && obj[prop][colon_index + 2] == '0') {
return obj[prop].slice(0, colon_index + 1);
}
return obj[prop];
}
用法:
obj = reduceTime(obj, 'start_time');
答案 1 :(得分:1)
timeSlot=[];
function reduceTime(st, et) {
var obj={
start_time:'',
end_time:''
};
if (st.indexOf(":00")>0){
obj.start_time=st.replace(":00", "")
}else{
obj.start_time=st;
}
if (et.indexOf(":00")>0){
obj.end_time=et.replace(':00', '')
}else{
obj.end_time=et;
}
timeSlot.push(obj)
}
用法:
for(i=0;i<response.length;i++){
reduceTime(response[i].start_time, response[i].end_time)
}
$scope.timeSlot=timeSlot;
然后我们将得到一个对象数组。